Laravel/PHP Articles

Articles from Laravel/PHP sites on the web

Laravel/PHP Articles
), 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.

Sign Up For Our Newsletter

Each Week, We curate the top Laravel/PHP articles, jobs, events and More!

We'll send you a weekly email with the latest Laravel/PHP articles, jobs and events. Please review our Code of Conduct in the meantime. Talk to you soon!