Laravel 8 Create Unique Slug Tutorial Example

In this tutorial you will learn about the Laravel 8 Create Unique Slug Tutorial Example and its application with practical example.

In this Laravel 8 Create Unique Slug Tutorial Example I will show you how to generate unique slug for blog posts title in laravel application. In this tutorial you will learn to generate unique slug for post title in laravel 8. In this example I will demonstrate you to generate unique slug or seo friendly slug url for post title in laravel 8. We will be using cviebrock eloquent sluggable laravel package for generating seo friendly url in laravel 8.

Laravel 8 Create Unique Slug Tutorial Example

In this step by step tutorial I will demonstrate you how to generate unique slug in laravel application. Please follow instruction given below:

  • 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 we need to create a fresh laravel project, download and install Laravel using the below command

Step 2 – Connecting App to Database

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.

Step 3 – Install Eloquent Sluggable Package

In this step, we will install Eloquent Sluggable Package via the composer dependency manager. Use the following command to install Eloquent Sluggable Package.

After installing eloquent sluggable package, run the following command:

Step 4 – Build Model and Migration

Now, in this step we will create model and migration file. Please run the following command:

The above command will create a model name Post and also create a migration file for the post table. Now put the following code in it:

Now, in this step we will create model and migration file. Please run the following command:

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

Step 5 – Add Routes

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Step 6 – Create Controller using Artisan Command

Now, lets create a controller named PostController using command given below –

Now, go to app/HTTP/Controller directory and open PostController.php file. And put the following code into it:

Step 7 – Create the blade view

Now we will create following blade files:

  • List.blade.php
  • Create.blade.php

List.blade.php

Create.blade.php

Step 8 – Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

In this tutorial we have learn about the Laravel 8 Create Unique Slug Tutorial Example and its application with practical example. I hope you will like this tutorial.