Laravel 8 create or generate unique slug. In this tutorial, you will learn how to create unique slug or seo friendly slug URL before save to database in laravel 8 app using the cviebrock eloquent sluggable laravel package.

Sometimes, you create a blog post project in laravel 8 app. At that time, need to create or generate unique slug for each blog post in laravel 8 app.

So, this tutorial will guide you step by step on generating or creating a unique slug for each post in your laravel 8 blog post project.

Create Unique Slug in Laravel 8

  • Step 1 – Install Laravel 8 App
  • Step 2 – Connecting App to Database
  • Step 3 – Install Eloquent Sluggable Package
  • Step 4 – Build Model and Migration
  • Step 5 – Add Routes
  • Step 6 – Create Controller using Artisan Command
  • Step 7 – Create the blade view
  • Step 8 – Start Development Server

Step 1 – Install Laravel 8 App

First of all, Execute the following command on terminal to install laravel 8 app. So, open your command prompt and execute the following command:

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

Step 2 – Connecting App to Database

In this step, Visit laravel 8 app project root directory and open .env file. Then add database credentials in the .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

Step 3 – Install Eloquent Sluggable Package

In this step, execute the following command on terminal install the eloquent sluggable package for generating unique slug:

composer require cviebrock/eloquent-sluggable

After successfully install eloquent sluggable package, type the below given command in command prompt:

php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider"

Step 4 – Build Model and Migration

In this step, execute the following command on terminal to create model and migration file:

php artisan make:model Post -m

It command will create one model name Post and also create one migration file for the post table. After successfully run the command go to database/migrations file and put the below here :

<?php

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

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('slug');
            $table->text('description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

Next, migrate the table using the below command:

php artisan migrate

Now, add the **fillable **property in Post.php file, which is placed inside app/models directory:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;

     protected $fillable = [
     'title',
     'slug',
     'description',
    ];
    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }
}

Step 5 – Add Routes

In this step, visit the routes directory and open web.php. Then add the following routes to web.php file:

<?php

 Route::get('/', function () {
     return view('welcome');
 });

Route::resource('posts', 'PostController');

#laravel #php #web-development #programming #developer

 How to Create Unique Slug in Laravel 8 App
68.65 GEEK