To create a slug in Laravel using the helper Str::slug. An example is given below.
use Illuminate\Support\Str;
use App\Models\Category;public function StoreCategory(Request $request){
$request->validate([
'category'=>'required','slug'=>'required',
],
['category.required'=>'Blog Category is required!!','slug.required'=>'Slug is required!!',
]);
Category::insert([
'blg_cat'=>$request->category,'slug'=>\Str::slug($request->category),
'created_at' => Carbon::now(),
]);
$notification = array(
'message' => 'Blogcategory Inserted Successfully',
'alert-type' => 'success'
);return redirect()->back()->with($notification);
}//End Method
But in this method, there is a duplicate value inserted. if the user set the category name twice as the same name, the slug will also be the same if you are not set it as a unique field otherwise you get an error.
Category.php Model file
protected $guarded = [];
Now I am going to show this package Install Eloquent Slugable Package
composer require cviebrock/eloquent-sluggable
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;class Post extends Model
{
use HasFactory;
use Sluggable;protected $guarded = [];
public function sluggable(): array
{
return [
'slug' => [
'source' => 'title'
]
];
}
}
Create a Controller as a PostController and paste the below code
<?php
namespace App\Http\Controllers\Home;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Post;
use Cviebrock\EloquentSluggable\Services\SlugService;
class PostController extends Controller
{
public function AddPost(){
return view(‘admin.post.add_post’);
}
public function StorePost(Request $request){
Post::create([
‘title’=>$request->title,
]);
$notification = array(
‘message’ => ‘POst added successfully done!!’,
‘alert-type’ => ‘success’
);
return redirect()->back()->with($notification);
}//End Method
public function getSlug(Request $request){
$slug= SlugService::createSlug(Post::class, ‘slug’, $request->title);
return response()->json([
‘slug’=>$slug
]);
}
}
web.php
Create a route
use App\Http\Controllers\Home\PostController;
Route::controller(PostController::class)->group(function(){
Route::get(‘/add/post’,’AddPost’)->name(‘add.post’);
Route::post(‘/store/post’,’StorePost’)->name(‘store.post’);
Route::get(‘/blog/slug’,’getSlug’)->name(‘blog.slug’);
});
And View file add_post.blade.php
@extends(‘admin.admin_master’)
@section(‘admin’)
<script src=”https://code.jquery.com/jquery-3.7.0.min.js”></script>
<div class=”page-content”>
<div class=”container-fluid”>
<div class=”row”>
<div class=”col-lg-6″>
<div class=”card”><br><br>
<div class=”card-body”>
<form action=”{{route(‘store.post’)}}” method=”post”>
@csrf
<div class=”row mb-3″>
<label for=”example-text-input” class=”col-sm-2 col-form-label”>Title</label>
<div class=”col-sm-10″>
<input class=”form-control” name=”title” id=”title” type=”text” >
<!– @error(‘title’)
<span class=”text-danger”> {{ $message }} </span>
@enderror –>
</div>
<label for=”example-text-input” class=”col-sm-2 col-form-label”>Slug</label>
<div class=”col-sm-10″>
<input class=”form-control” name=”slug” id=”slug” type=”text” >
<!– @error(‘title’)
<span class=”text-danger”> {{ $message }} </span>
@enderror –>
</div>
</div>
<input type=”submit” class=”btn btn-info” value=”Add POst”>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(‘#title’).change(function(){
$.ajax({
url:'{{route(“blog.slug”)}}’,
type:’get’,
data:{title: $(this).val()},
dataType: ‘json’,
success:function(data){
$(‘#slug’).val(data.slug);
}
});
});
</script>
@endsection
And don’t forget to add jquery cdn.
Leave a Comment
No Comments found