In this tutorial you will learn about the Laravel 7/6 Create Newsletter Example Tutorial and its application with practical example.
In this Laravel 7/6 Create Newsletter Example Tutorial I’ll show you how to implement newsletter functionality in laravel application. In this tutorial you will learn to create newsletter feature in your laravel application. In this example we will be using MailChimp for setting up newsletter functionality in laravel project.
Laravel 7/6 Create Newsletter Example Tutorial
- Install Laravel Fresh New Setup
- Setup Database Credentials
- Install Newsletter Package
- Sign Up in MailChimp Get MailChimp API Key And List Id
- Set MailChimp API Key And List Id in .env file
- Create Resource Route & Controller
- Create the blade view
- Start Development Server
1. Install Laravel Fresh New Setup
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel Blog |
2. Setup Database Credentials
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
3. Install Newsletter Package
In this step, we will install newsletter Package via the composer dependency manager. Use the following command to install newsletter Package.
1 |
composer require spatie/laravel-newsletter |
The package will automatically register itself. Use following command to publish the config file to config/newsletter.php:
1 |
php artisan vendor:publish --provider="Spatie\Newsletter\NewsletterServiceProvider" |
4. Sign Up in MailChimp Get MailChimp API Key And List Id
Now, we have to sign up in MailChimp from https://mailchimp.com/. If you already have Mailchimp account then login directly. After successfully sign up or sign we can get api key and list id from mailchimp
5. Set MailChimp API Key And List Id in .env file
In this step we will set Mailchimp API key and list id in .env file:
1 2 |
MAILCHIMP_APIKEY=xxxx MAILCHIMP_LIST_ID=xxxx |
6. Create Resource Route & Controller
Now, we will create a resource route along with controller:
1 |
php artisan make:controller NewsletterController --resource |
This command will create a controller name NewsletterController and also inside by default seven methods like index, store.Next, We need to add the resource route. Go to routes/web.php put the below routes here :
1 2 |
Route::get('newsletter','NewsletterController@create'); Route::post('newsletter','NewsletterController@store'); |
Next open controller, Go to app/HTTP/Controller/NewsletterController and put the below code here :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?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 '); } } |
7. Create the blade view
In this step, we will create a blade file, Go to app/resources/views/ and create one blade view name newsletter.blade.php. After creating the blade file, put the given code into your newsletter.blade.php file:
newsletter.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Laravel 6 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')}}"> @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> |
8. Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/newsletter |