PHP Developers Network
http://forums.devnetwork.net/

How to generate slug in laravel ??
http://forums.devnetwork.net/viewtopic.php?f=72&t=143274
Page 1 of 1

Author:  gautamz07 [ Mon Jan 09, 2017 11:04 am ]
Post subject:  How to generate slug in laravel ??

I currently have the following foreach loop running on the index page of my application:

Syntax: [ Download ] [ Hide ]
@foreach($recentPost as $recent)

        <div class="col-md-6">
                <div class="indi-news-highlight-box">
                        <a href="{!!  route('getArticle' , ['id' => $recent->id ]) !!}">
                                <img src="{{ URL::asset('images/temp/blog-post-image.jpg') }}" alt="{!!  $recent->title  !!}">
                        </a>
                        <div class="content-box">
                                <h3>
                                        <a href="{!!  route('getArticle' , ['id' => $recent->id ]) !!}">
                                                {{  $recent->title }}
                                        </a>
                                </h3>
                                <p>
                                        {{ $recent->blog_content }}
                                </p>
                        </div>         
                </div>
        </div>   <!-- End col-md-6 -->

@endForEach
 


Now as you can see the anchor tags are populated with the id which is numbers like 1,2,3 etc...

so i have the following route in my route file:

Syntax: [ Download ] [ Hide ]
Route::get('/{id}' , [
        'uses' => 'PagesController@show',
        'as' => 'getArticle'
]);
 


and then the following method in my pagesController:

Syntax: [ Download ] [ Hide ]
public function show($id) {
        return view('pages.article' , ['article' => Admin::findOrFail($id)]);
}
 


So now when i click on the links i get urls like so:
http://localhost:8080/laravel-blog/public/6
http://localhost:8080/laravel-blog/public/1

and so on.. but what i really want is the slug of the article , so say the article title is 'firefox browser bug' ,

i want the url to be:
http://localhost:8080/laravel-blog/publ ... rowser-bug

How do i do this ??

Thank you.
Gautam.

Author:  Celauran [ Mon Jan 09, 2017 1:53 pm ]
Post subject:  Re: How to generate slug in laravel ??

Do you have such a route defined? What have you tried so far?

Author:  gautamz07 [ Tue Jan 10, 2017 12:08 am ]
Post subject:  Re: How to generate slug in laravel ??

What should i search for on google , i am not sure .. and if i want a slug .. instead of an ID , what exactly do i insert in the href of the anchor tag ? i am currently doing :

Syntax: [ Download ] [ Hide ]
<a href="{!!  route('getArticle' , ['id' => $recent->id ]) !!}">


can you give me a hint :P

Author:  Celauran [ Tue Jan 10, 2017 8:26 am ]
Post subject:  Re: How to generate slug in laravel ??

Do you understand how routing works? How Eloquent models work? I'd read up on those first. Think about what you're trying to do, what your find needs to look like, what parameters you need to pass to accomplish that.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/