Category: Laravel

), it’s convenient to generate slug immediately after title has been typed in.
public function check_slug(Request $request) { $slug = str_slug($request->title); return response()->json(['slug' => $slug]); }Simple, right?
* * @return array */ public function sluggable() { return [ 'slug' => [ 'source' => 'title' ] ]; } }We’ve created a method that defines to generate slug field from title field of pages database table.
And now, final step, we can replace old “simple” method of generating slug in PagesController:use Cviebrock\EloquentSluggable\Services\SlugService; public function check_slug(Request $request) { // Old version: without uniqueness $slug = str_slug($request->title); // New version: to generate unique slugs $slug = SlugService::createSlug(Page::class, 'slug', $request->title); return response()->json(['slug' => $slug]); }And, that’s it!
Oh, and don’t forget to still check uniqueness on back-end validation when doing store() for the page, add rule ‘slug’ => unique:pages in validation.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Community Partners