Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

generate a slug without a package in Laravel 9

How to generate a slug without a package in Laravel 9?

In this tutorial, I will give you an example of “How to Generate a slug without a package in Laravel 9”, So you can easily apply it with your Laravel 9 application.

First, what we’re doing here, This is the example :

Generate slug in laravel 9
generate slug without any package in laravel 9

Generate Slug in Laravel 9

We have many options to use slug instead of id in Url and get the data using slug from URL, to create unique slugs for every post or page in Laravel. The slug works like the Primary Key and it will be always unique.

Related article :- How to Generate Unique Slug using sluggable package in Laravel.

So, let’s start with scratch to Generate a Unique slug From the Title in Laravel 9 :

Generating Migration with Model :

php artisan make:model Post -m

Migration Structure :

<?php

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

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

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

Run Migration :

php artisan migrate
generate slug using title in laravel

app\Models\Post.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Str;

class Post extends Model
{
    use HasFactory;

    public function title() : Attribute {
        return new Attribute(
            set: fn($value) => [
                'title' => $value,
                'slug'  => Str::slug($value)
            ]
        );
    }
}

Create a Controller :

php artisan make:controller PostController

routes\web.php :

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;

 
Route::get('/create-post',[PostController::class,'create']);
Route::post('/store-post',[PostController::class,'store'])->name('store.post');

app\Http\Controllers\PostController.php :

<?php

namespace App\Http\Controllers;

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

class PostController extends Controller
{

    public function create()
    {
       return view('post.create');
    }

    public function store(Request $request)
    {
       $post = new Post();
       $post->title = $request->title;
       $post->content = $request->content;
       $post->save();
       return 'success';
    }
}

resources\views\post\create.blade.php :

Laravel unique slug in laravel 9
 <div class="container">
   <div class="row justify-content-center">
      <div class="col-lg-8">
      <div class="main">
      <h3>How to Generate slug without any Package in Laravel 9</h3>
      <form action="{{ route('store.post') }}" method="post" enctype="multipart/form-data">
         @csrf
         <div class="form-group">
            <label for="title">Title<span class="text-danger">*</span></label>
            <input type="text" name="title" class="form-control" required>
         </div>
         <div class="form-group">
            <label for="content">Content<span class="text-danger">*</span></label>
            <input type="text" name="content" class="form-control" required>
         </div>
         <div class="form-group">
            <button type="submit" class="btn btn btn-info">Save Post</button>
         </div>
      </form>
      </div>
      </div>
   </div>
</div>

Output:

Laravel 9 Create Unique Slug Example

In this article, we learned “How to Generate a slug without a package in Laravel 9”, I hope this article will help you with your Laravel application Project.