Laravel Create Newsletter Example Tutorial

10-Apr-2023

.

Admin

Laravel Create Newsletter Example Tutorial

Hi Guys,

In this tutorial,I will learn you how to use newsletter in laravel.you can easy and simply use newsletter in laravel.

Step 1 : Install Laravel App


In this step, You can install laravel fresh app. So open terminal and put the bellow command.

composer create-project --prefer-dist laravel/laravel blog

Step 2 : Setup Database Configuration

After successfully install laravel app thenafter configure databse setup. We will open ".env" file and change the database name, username and password in the env file.

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=Enter_Your_Database_Name

DB_USERNAME=Enter_Your_Database_Username

DB_PASSWORD=Enter_Your_Database_Password

Step 3: Install Newsletter Package

In this step,you can install newsletters in command terminal.bellow this command.

composer require spatie/laravel-newsletter

The package will automatically register itself.

To publish the config file to config/newsletter.php run:

php artisan vendor:publish --provider="Spatie\Newsletter\NewsletterServiceProvider"

Step 4: Sign Up in MailChimp Get MailChimp API Key And List Id

Now, you sign up in MailChimp from https://mailchimp.com/. If you have already account then sign in. After successfully sign up or sign we can get api key and list id from mailchimp

Step 5: SignSet MailChimp API Key And List Id in .env file

Next, we need to update the Mailchimp API key and list id in.env file:

MAILCHIMP_APIKEY=xxxx

MAILCHIMP_LIST_ID=xxxx

Step 6: Create Route

now, we need to add for NewsletterController in laravel application. so open your "routes/web.php" file and add following route.

routes/web.php

Route::get('newsletter','NewsletterController@index');

Route::post('newsletter/store','NewsletterController@store');

Step 7: Create Controler

Here this step now we should create new controller as NewsletterController. So run bellow command and create new controller.

php artisan make:controller NewsletterController

successfully run above command then,you can create method for get courses and fetch record students table. So Let's copy bellow and put in the controller file.

app/http/controller/NewsletterController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Newsletter;

class NewsletterController extends Controller

{

public function index()

{

return view('newsletter');

}

public function store(Request $request)

{

if ( ! Newsletter::isSubscribed($request->email) )

{

Newsletter::subscribePending($request->email);

return redirect('newsletter')->with('success', 'Thanks For Subscribe');

}

return redirect('newsletter')->with('failure', 'Sorry! You have already subscribed ');

}

}

Step 8: Create Blade File

In this step,you can create to layout in this blade file.

/resources/views/newsletter.blade.php

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Laravel Newsletter Tutorial With Example</title>

<link rel="stylesheet" href="{{asset('css/app.css')}}">

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

</head>

<body>

<div class="container">

@if (\Session::has('success'))

<div class="alert alert-success">

<p>{{ \Session::get('success') }}</p>

</div><br />

@endif

@if (\Session::has('failure'))

<div class="alert alert-danger">

<p>{{ \Session::get('failure') }}</p>

</div><br />

@endif

<h2 class="mb-2 mt-2">Laravel Newsletter Tutorial With Example</h2>

<form method="post" action="{{url('newsletter/store')}}">

@csrf

</div>

<div class="row">

<div class="col-md-8"></div>

<div class="form-group col-md-2">

<label for="Email">Email:</label>

<input type="text" class="form-control" name="email">

</div>

</div>

<div class="row">

<div class="col-md-4"></div>

<div class="form-group col-md-4">

<button type="submit" class="btn btn-success">Submit</button>

</div>

</div>

</form>

</div>

</body>

</html>

Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/newsletter

It will help you....

#Laravel 7

#Laravel

#Laravel 6