PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Apr 30, 2017 7:55 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Jan 09, 2017 11:04 am 
Offline
Forum Contributor
User avatar

Joined: Wed May 14, 2014 12:18 pm
Posts: 316
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.


Top
 Profile  
 
PostPosted: Mon Jan 09, 2017 1:53 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6229
Location: Montreal, Canada
Do you have such a route defined? What have you tried so far?

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Tue Jan 10, 2017 12:08 am 
Offline
Forum Contributor
User avatar

Joined: Wed May 14, 2014 12:18 pm
Posts: 316
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


Top
 Profile  
 
PostPosted: Tue Jan 10, 2017 8:26 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6229
Location: Montreal, Canada
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.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group