armancs's avatar

How to use slug in laravel?

I have 3 tables called country,university and university info. Now there is a country list page when i enter a country all university will display according to that country.after clicking an university a university information will display. How can i make it with using slug?

0 likes
5 replies
armancs's avatar

@cronix Thanks for your reply. I have no slug column. I want to made url from university_table where all university list already appeared. and some model related with each other. i have no idea how can i use it.

Chaeril's avatar

i use attribute for it, but still not sure which is better practice

protected $appends = ['slug' ];

    public function getSlugAttribute()
    {
        return str_slug(strtolower($this->attributes['name']));
    }
NOMGUY's avatar

Do you want to show your slug in the url bar??

NOMGUY's avatar

if so, no need to create any field in database or something like that. all you have to do is like this: This is in index.blade.php

<h2>
<a href="{{ route('posts.show',[$post->id."-".str_slug($post->title)]) }}">{{ $post->title }}</a>
</h2>

if you post title is Hello World and the id is 4, then in the url it will show like, www.example.com/show/4-hello-world

and if you want to show it on some page, all you need to do is,

<p>{{ str_slug('$post->title) }}</p>

Please or to participate in this conversation.