Laravel Create Newsletter Example Tutorial

Admin   Laravel   396  2021-03-15 22:00:08

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','[email protected]');

Route::post('newsletter/store','[email protected]');

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