Laravel 7/6 Create Newsletter Example Tutorial
Laravel 08-Dec-2020

Laravel 7/6 Create Newsletter Example Tutorial

Laravel 6 creates a newsletter example tutorial. In this tutorial, we will learn how to create newsletter in laravel application with example.

This example tutorial also work with laravel 7.x version.

Laravel Create Newsletter Example Tutorial

Just follow the below steps and create a newsletter in laravel application

  • 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

We need to install laravel 6 fresh application using below command, Open your command prompt and run the below command :

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

After successfully install laravel 6 Application, Go to your project .env file and set up database credential and move next step.

2. Setup Database Credentials

In this step, we will set database credentials in the .env file. Let’s open .env file.

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

Next, open a command prompt and go to your project root directory like cd/project name. And type the below command to install Newsletter via the Composer package manager:

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"    

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

5. Set 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 

6. Create Resource Route & Controller

Create the NewsletterController using the below command:

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 :

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 :

<?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 need to create blade view files, Go to app/resources/views/ and create one blade view name newsletter.blade.php.

After successfully create the blade view file, update the below-given code into your newsletter.blade.php file:

newsletter.blade.php

<!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

In this step, we will use the PHP artisan serve command. It will start your server locally

php artisan serve

If you want to run the project diffrent port so use this below command 

php artisan serve --port=8080  

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

http://localhost:8000/newsletter

Conclusion

In this article, We have learned how to create a newsletter in laravel using the laravel newsletter package with example. Our examples run quickly.