Hello I'm trying to create own laravel package it has two Controller resource, which have single controller and model Post
Route::resource('posts', \vendor\package\Controllers\PostsController::class);
Route::resource('categories', \vendor\package\Controllers\PostsController::class);
My method in the PostsController
is show(Post $post)
If I open link http://localhost/posts/1, attributes element of $post
is not empty in the show
method.
But when I open link http://localhost/categories/1, attributes element of $post
is empty.
How can I get Post data for resource
categories
?
P.S. difference between posts and categories is value of column post_type
in the DB.
You can define what the route parameter will be named for the generated routes when using resource routing.
This should be the only change you need to make:
Route::resource(
'categories',
\vendor\package\Controllers\PostsController::class,
['parameters' => ['categories' => 'post']]
);
Now the route parameter is post
:
GET categories/{post}
GET categories/{post}/edit
...
Laravel 5.6 Docs - Controllers - Naming Resource Route Parameters