Laravel: Generate SEO-Friendly URL Slug Automatically

Laravel: Generate SEO-Friendly URL Slug Automatically

Laravel Daily

4 года назад

31,189 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Jagpal Singh
Jagpal Singh - 06.08.2022 05:49

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

Ответить
shamim dewan
shamim dewan - 08.12.2021 07:02

thanks

Ответить
Jakob Kichel
Jakob Kichel - 27.03.2021 17:47

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??

Ответить
Marco Bonanno
Marco Bonanno - 21.03.2021 17:39

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

Ответить
Zeeshan Haider
Zeeshan Haider - 08.03.2021 08:49

What is the use of trans() in blade as you used in label?

Ответить
Aleksandrs Kauhovs
Aleksandrs Kauhovs - 05.03.2021 14:53

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 = Str::slug($this->attributes['name']);

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.

Ответить
Dane Elsdon
Dane Elsdon - 19.02.2021 12:56

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

Ответить
Ajinkya More
Ajinkya More - 05.02.2021 03:42

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

Ответить
Pawana
Pawana - 30.10.2020 18:52

Assuming that you know how to validate in Livewire, you can make this to work with Livewire's live validation by adding this function to the component:

public function updatedTitle($value)
{
$slugged = SlugService::createSlug(Page::class, 'slug', $value);
$this->slug = $slugged;
}

The name of the function is mandatory for this to automatically fire up when a change is made in the input with the wire:model="title" in the form view.

Ответить
Muhammad Yasir
Muhammad Yasir - 29.09.2020 13:15

Nice:)

Ответить
Amit Chandrakar
Amit Chandrakar - 24.06.2020 06:05

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.

Ответить
Marndi
Marndi - 19.06.2020 14:05

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

Ответить
Fouèd MOUSSI
Fouèd MOUSSI - 30.05.2020 10:26

Can this work properly with non-latin strings ?

Ответить
Mel Reyes
Mel Reyes - 27.05.2020 06:31

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

Ответить
Alireza Panahi
Alireza Panahi - 14.05.2020 04:50

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

Ответить
latlov
latlov - 01.05.2020 15:55

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

Ответить
Tahir Bhai
Tahir Bhai - 30.04.2020 17:55

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.

Ответить