Hello Artisan
In this Laravel use slug instead of id tutorial, I am going to show you the use case of slug in Laravel. I will give you a complete example of using Laravel slug by creating a crud application. So from this tutorial, you will learn how to use a slug in Laravel and how to use a slug in the Laravel route instead of id.
So we will generate a slug from the title before saving. After saving the data we will update, delete, and will show single data using that slug. So if you don't know how to use slug in Laravel then this example is perfect for you. So let's start our tutorial that how to use a slug in Laravel.
Before Laravel 7 is released, we can use slug which is slightly different from Laravel 7 and 8 versions. To know the difference, you can read the below content.
Recommended: Laravel 7.x Route Model Binding Improvements
I am going from scratch to complete a crud application using Laravel slug. So let's start.
Step 1: Download Laravel
Download a Laravel project to start our how to use slug in Laravel. Run below command:
laravel new test
Step 2: Create Migration
Now we have to create a tags
table with tag_slug
to show you how we can use that slug to show and delete it.
php artisan make:model Tag -m
And update the Tag model like that:
app\Models\Tag.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
use HasFactory;
protected $fillable = [
'tag_name',
'tag_slug',
'tag_description',
];
}
Now update your table schema like that:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('tag_name')->unique();
$table->string('tag_slug')->unique();
$table->string('tag_description')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tags');
}
}
Now run php artisan migrate
command to migrate this table.
Step 3: Create Route
In this Laravel use slug step by step tutorial, we have to create some routes to complete crud application with Laravel using slug.
routes/web.php
use App\Http\Controllers\Admin\TagController;
Route::name('admin.')->prefix('admin')->group(function () {
Route::get('tag', [TagController::class,'index'])->name('tag.index');
Route::get('tag/create', [TagController::class,'create'])->name('tag.create');
Route::post('tag/store', [TagController::class,'store'])->name('tag.store');
Route::get('tag/edit/{tag:tag_slug}', [TagController::class,'edit'])->name('tag.edit');
Route::patch('tag/edit/{tag:tag_slug}', [TagController::class,'update'])->name('tag.update');
Route::delete('tag/delete/{tag:tag_slug}', [TagController::class,'delete'])->name('tag.delete');
});
Step 4: Create Controller
Now we need to create TagController to write those methods. So run php artisan make:controller TagController
command to create controller and update it like below:
namespace App\Http\Controllers\Admin;
use App\Models\Tag;
use App\Helper\Helper;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class TagController extends Controller
{
public function index(Tag $tag)
{
return view('admin.tag.index',[
'tags' => $tag->orderBy('id','desc')->get()
]);
}
public function create()
{
return view('admin.tag.create');
}
public function store(Request $request)
{
$request->validate([
'tag_name' => 'required|unique:tags'
]);
Tag::create([
'tag_name' => $request->tag_name,
'tag_slug' => Str::slug($request->tag_name),
'tag_description' => $request->tag_description
]);
return Helper::success("{$request->tag_name} tag created successfully");
}
public function edit(Tag $tag)
{
return view('admin.tag.edit',[
'tag' => $tag
]);
}
public function update(Request $request, Tag $tag)
{
$request->validate([
'tag_name' => 'required'
]);
$tag->update([
'tag_name' => $request->tag_name,
'tag_description' => $request->tag_description
]);
return Helper::success("{$request->tag_name} tag updated successfully");
}
public function delete(Tag $tag)
{
$tag->delete();
return Helper::success("{$tag->tag_name} tag deleted successfully");
}
}
Now create a Helper class like that:
App\Helper\Helper.php
namespace App\Helper;
class Helper
{
public static function success(string $message)
{
return redirect()->back()->with('success', ucfirst($message));
}
}
Now all are set to go, we have to just create our blade views.
Step 5: Create Blade View
In this step, we have to show our data to update and delete using slug. So we need to create some views. So create it and update it like that:
resources/views/admin/tag/index.blade.php
Recommended: Process to Generate Unique Slug Example in Laravel
Now for creating data, we need to create files like:
resources/views/admin/tag/create.blade.php
And for update, we need edit file like that:
resources/views/admin/tag/edit.blade.php
Now all are ok, you can test now.
Hope it can help you.
#laravel #laravel-8x #slug