php - How could I have 2 different route keys names for my Post model? (Laravel)
Get the solution ↓↓↓I've made a simple Laravel CRUD, and I'm stuck on a problem.
My posts have aslug
column and auuid
column in the table in which they are stored. I've binded the routes to thePostController
using
Route::resource("posts", "PostController");
I want to be able to call theshow
route by using the slug in the URL, (example.com/posts/all-about-my-new-banana-maker
), but to be able to call theedit
route by using the UUID (example.com/posts/ddd83f7b-9c73-11eb-93c2-e55e28ace783/edit
)
I've tried editing myPost
model and changinggetRouteKeyName
:
class Post extends Model
{
use HasFactory;
// Set mass-assignable fields
protected $fillable = ["title", "content", "category", "image", "slug", "uuid"];
/**
* Get the route keyfor the model
* @return string
*/
public function getRouteKeyName() {
return "slug";
}
}
I can only see to have UUID or slug, not both at the same time. How could I make it so that both UUID and slug retrieve my post?
Answer
Solution:
If you want to pass extra route parameter, then you need to define an another route for that :
Route::get("posts/{uuid}/{slug}", "PostController@data");
And your controller will be looked like :
public function data($uuid, $slug) {
// You can access $uuid and $slug here
}
Answer
Solution:
You can either remove any type hints from your methods, or override the route model binding in yourRouteServiceProvider
.
Remove type hinting
public function show($post)
{
$post = Post::where('slug', $post)->firstOrFail();
}
public function edit($post)
{
$post = Post::where('uuid', $post);
}
Override route model binding
Add something like the following to theboot
method of theRouteServiceProvider
.
Route::bind('post', function ($value){
return Post::where('slug', $value)->orWhere('uuid', $value)->firstOrFail();
});
You still use type hinting if you override the route model binding in theRouteServiceProvider
.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: invalid argument supplied for foreach() laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.