Laravel: Generate SEO-Friendly URL Slug Automatically

preview_player
Показать описание
Small tip on how to generate URL like "my-post" from the title "My Post". We will use a package called Eloquent Sluggable.

- - - - -
Рекомендации по теме
Комментарии
Автор

i am using quil js editor for blogs in my laravel project and i want to add nofollow links in this editor as we are using in wordpress. i try but not work can you give me any solution about this .

nomanbashir
Автор

I am trying all the time but cant figure out how to fill the Slug-field with the new generated slug at the create blade. i am getting the slug and everything but the slug-field doesnt automaically fill itself. is there kind a trick to do it??

jakobkichel
Автор

Hi, how to use the slug in url instead of the post id with this case?
Regards

daneelsdon
Автор

thank you for all your tutorials, i learned a lot.

melreyes
Автор

Some time duplicate slug generates and gives errors constraint violation: 1062 Duplicate entry.

marndi
Автор

Thank you for the info, by the way you can use the same exact ajax request, while editing the slug manually, using the jquery events like keyUp, blue or anything else. no need to hit the submit button for check. I think it will be more efficient.

TahirBhai
Автор

Hi, I have a question. Why we need to use package to generate a slug? We can easily achieve this by using jquery. We just need jquery function which will take title text and replace all the special characters, symbols with "-" . It will be easier than using a separate package, I guess.

Please correct me if I'm wrong.

amitchandrakar
Автор

Can this work properly with non-latin strings ?

fouedmoussi
Автор

Nice! Can we use this same principle for real time validation? What would it be better to do real time validation with livewire?

latlov
Автор

But we can use laravel's string helper class to generate slugs using slug() function Str::slug(). They why we need this third party library?

ajithlal
Автор

1:40
For anyone who didn't want to use the external package I've made this solution:
I'm using it in "checkSlug" Function..

public function checkSlug(Request $request)
{
$slug =
if (Post::where('slug', $slug)->exists()) {
$slug = $slug . '-' . Str::random(2);
return response()->json(['slug' => $slug]);
} else {
return response()->json(['slug' => $slug]);
}
}

Happy Coding ^_^

yasser.elgammal
Автор

Why do you still use Jquery? We're in the middle of 2020!

apairca
Автор

Hello respected sir title and slug same fill up...plz

jagpalsingh
Автор

I have this question!
How to open a page in a browser without a category?
Now all pages are opened (/category/post), but how to do this (/post)?
Here is how I wrote
web.php
Route::get('{post_slug}', [App\Http\Controllers\Frontend\FrontendController::class, 'viewsPost']);
FrontendController.php
public function viewsPost(string $post_slug) {
$post = Post::where('status', '0')->get();
return view('frontend.post.views', compact('post'));
}

TheWorldGame
Автор

Hi, can you make a video on how to handle slugs in languages like arabic or indian etc?

MarcoBonannowebsite
Автор

Can you please do a tutorial on Laravel Telescope?

ricardovolvox
Автор

Thank you so much! Really helped me alot! :)

ajinkya-more
Автор

Came across this tutorial, just if some one don`t want to use extra package, I`m using mutator on model, for example Product model:

public function setSlugAttribute($value)
{
if(!$value) {
$slug =

if(Product::where('slug', $slug)->exists())
{
$i = 0;
do
{
$i++;
$slug_result = $slug.'-'.$i;
} while (Product::where('slug', $slug_result)->exists());
} else {
$slug_result = $slug;
}
$this->attributes['slug'] = $slug_result;
} else {
$this->attributes['slug'] = $value;
}
}


maybe not super optimized code, but does the trick.

SanjaHD