I am trying to make my URL more SEO friendly on my Laravel application by replacing the ID number of a certain object by the name on the URL when going to that specific register show page. Anyone knows how?
This is what I got so far and it displays, as normal, the id as the last parameter of the URL:
web.php
Route::get('/job/show/{id}', ['as'=>'website.job.show','uses'=>'HomeController@show']);
Controller method
public function show($id){
$job = Job::findOrFail($id);
return view('website.job')->with(compact('job'));
}
Blade page where there is the link to that page
<a href="{{route('website.job.show', $job->id)}}">{{$job->name}}</a>
3
Answers
You need simply to replace the
id
by thename
:In the controller action:
Finally in the blade page :
You can overwrite the key name of your
Job
model:Then in your route simply use
{job}
:And to call your route:
So your
a
tag would look like this:Inside your controller, you can change the method’s signature to receive the Job automatically:
For more information, look at customizing the key name under implicit binding: https://laravel.com/docs/5.8/routing#implicit-binding
2 options:
1) one is like @zakaria-acharki wrote in his comment, by the name of the job and search by the name for fetching the data
2) the second is to do it like here in stackoverflow
to build the url with the id/name
in this way you will make sure to fetch and show the relevant job object by the unique ID
the route:
in the controller, update the check if the name is equal to the job name (in case it was changed) to prevent duplicate pages url’s
in the blade.php :