In this tutorial you will learn about the Laravel 8 Generate Unique Slug URL Example Tutorial and its application with practical example.
In this Laravel 8 Generate Unique Slug URL Example Tutorial I will show you how to generate multiple unique slug URLs in the Laravel 8 application. In this tutorial you will learn to generate unique seo friendly slug URLs in the Laravel 8 application and store slugs in the database using the id. In this example I will demonstrate you to generate unique slug or seo friendly slug url for post in laravel 8.
Laravel 8 Generate Unique Slug URL Example Tutorial
In this step by step tutorial I will demonstrate you how to Generate unique Slug URL in laravel. Please follow the instruction given below:
- Step 1: Download New Laravel Project
- Step 2: Update Database Details
- Step 3: Create Migration and Model
- Step 4: Create Controller File
- Step 5: Register New Route
- Step 6: View App in Browser
Download New Laravel Project
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel laravel-demo |
Update Database Details
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=DB_Name DB_USERNAME=DB_Username DB_PASSWORD=DB_Password |
Create Migration and Model
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Blog -m |
Now open app/Models/Blog.php file, and define the values that you want to add in database table.
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 42 43 44 45 |
<?php namespace App\Models; use Illuminate\Support\Str; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Blog extends Model { use HasFactory; protected $fillable = [ 'title', 'slug', 'details' ]; public $timestamps = false; protected static function boot() { parent::boot(); static::created(function ($blog) { $blog->slug = $blog->initSlug($blog->title); $blog->save(); }); } private function initSlug($title) { if (static::whereSlug($slug = Str::slug($title))->exists()) { $max = static::whereName($title)->latest('id')->skip(1)->value('slug'); if (isset($max[-1]) && is_numeric($max[-1])) { return preg_replace_callback('/(\d+)$/', function($mathces) { return $mathces[1] + 1; }, $max); } return "{$slug}-1"; } return $slug; } } |
Open create_blogs_table.php migration file and update the function up() method as following:
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 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateBlogsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('blogs', function (Blueprint $table) { $table->id(); $table->string('title', 120); $table->string("slug", 120); $table->text('details'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('blogs'); } } |
Create Controller File
Now, lets create a controller named BlogController using command given below –
1 |
php artisan make:controller BlogController |
Once the above command executed, it will create a controller file BlogController.php in app/Http/Controllers/ directory. Open the BlogController.php file and put the following code in it.
app/Http/Controllers/BlogController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Blog; class BlogController extends Controller { public function index() { $blog = Blog::create([ "title" => "The demo is in process", "slug" => "laravel-multiple-slug-urls-example", "details" => "Here is the slug demo" ]); dd($blog); } } |
Register New Route
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
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\BlogController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- */ Route::get('/blog', [BlogController::class, 'index']); |
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://127.0.0.1:8000/blog |