How to create Newsletter in laravel 11 | websolutioncode.com
How to create Newsletter in laravel 11 | websolutioncode.com

How to create Newsletter in laravel 11

In this tutorial, we will walk through the process of creating a newsletter system using Laravel 11. Newsletters are a great way to keep your audience engaged and informed about the latest updates, promotions, or news related to your business or organization. By the end of this tutorial, you will have a fully functional newsletter system that allows users to subscribe, unsubscribe, and receive emails.

Step 1: Setting Up Laravel

Before we start building our newsletter system, make sure you have Laravel installed on your system. If not, you can install it by following the instructions on the official Laravel documentation.

Once Laravel is installed, create a new Laravel project by running the following command in your terminal:

laravel new newsletter

Navigate to the newly created project directory:

cd newsletter

Step 2: Database Configuration

Next, we need to set up our database configuration. Open the .env file in your project directory and configure your database settings:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password

Make sure to replace your_database_name, your_database_username, and your_database_password with your actual database credentials.

Step 3: Creating Models and Migrations

Now, let’s create the necessary models and migrations for our newsletter system. Laravel utilizes migrations to establish database tables.

Run the following Artisan command to create a migration for the subscribers table:

php artisan make:migration create_subscribers_table

This will create a new migration file in the database/migrations directory. Open the migration file and define the schema for the subscribers table:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSubscribersTable extends Migration
{
public function up()
{
Schema::create('subscribers', function (Blueprint $table) {
$table->id();
$table->string('email')->unique();
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('subscribers');
}
}

Next, run the migration to create the subscribers table in the database:

php artisan migrate

Now, let’s create a model for the Subscriber:

php artisan make:model Subscriber

This will create a Subscriber.php file in the app/Models directory.

Step 4: Creating Routes

Now, let’s define the routes for subscribing and unsubscribing from the newsletter. Open the routes/web.php file and add the following routes:

use App\Http\Controllers\NewsletterController;

Route::post('/subscribe', [NewsletterController::class, 'subscribe'])->name('subscribe');
Route::get('/unsubscribe', [NewsletterController::class, 'unsubscribe'])->name('unsubscribe');

Step 5: Creating Controller

Next, let’s create a controller to handle the newsletter subscription logic. Run the following command to create a new controller:

php artisan make:controller NewsletterController

Open the NewsletterController.php file in the app/Http/Controllers directory and add the following code:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Subscriber;

class NewsletterController extends Controller
{
public function subscribe(Request $request)
{
$request->validate([
'email' => 'required|email|unique:subscribers,email'
]);

Subscriber::create([
'email' => $request->email
]);

return redirect()->back()->with('success', 'You have subscribed to our newsletter.');
}

public function unsubscribe(Request $request)
{
$request->validate([
'email' => 'required|email'
]);

Subscriber::where('email', $request->email)->delete();

return redirect()->back()->with('success', 'You have unsubscribed from our newsletter.');
}
}

Step 6: Creating Views

Finally, let’s create the views for subscribing and unsubscribing from the newsletter. Create a new directory called newsletter inside the resources/views directory. Inside the newsletter directory, create two blade templates: subscribe.blade.php and unsubscribe.blade.php.

In subscribe.blade.php:

<!DOCTYPE html>
<html>
<head>
<title>Subscribe to Newsletter</title>
</head>
<body>
<h1>Subscribe to Newsletter</h1>

@if(session('success'))
<p>{{ session('success') }}</p>
@endif

<form method="POST" action="{{ route('subscribe') }}">
@csrf
<input type="email" name="email" placeholder="Enter your email" required>
<button type="submit">Subscribe</button>
</form>
</body>
</html>

In unsubscribe.blade.php:

<!DOCTYPE html>
<html>
<head>
<title>Unsubscribe from Newsletter</title>
</head>
<body>
<h1>Unsubscribe from Newsletter</h1>

@if(session('success'))
<p>{{ session('success') }}</p>
@endif

<form method="GET" action="{{ route('unsubscribe') }}">
@csrf
<input type="email" name="email" placeholder="Enter your email" required>
<button type="submit">Unsubscribe</button>
</form>
</body>
</html>

Step 7: Testing

You can now test your newsletter system by visiting the subscribe and unsubscribe routes in your browser.

That’s it! You have successfully created a newsletter system in Laravel 11. Users can now subscribe and unsubscribe from your newsletter, and you can send emails to your subscribers to keep them informed about your latest updates and promotions.