How to generate slug in laravel ??
Posted: Mon Jan 09, 2017 10:04 am
I currently have the following foreach loop running on the index page of my application:
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:
and then the following method in my pagesController:
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.
Code: Select all
@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
so i have the following route in my route file:
Code: Select all
Route::get('/{id}' , [
'uses' => 'PagesController@show',
'as' => 'getArticle'
]);
Code: Select all
public function show($id) {
return view('pages.article' , ['article' => Admin::findOrFail($id)]);
}
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.