Route:
Route::get('/posts/{post}', [PostController::class, 'show']);
Controller:
public function show(Post $post){
$postData = $post->load(['author' => function($query){
$query->select('post_id', 'name');
}])
->get(['title', 'desc', 'created_date'])
->toArray();
}
This returns all the posts in the database, While I only want to get the selected post passed to the show
function.
So if I visit /posts/3
, It should show data related to the post with id
= 3, not all posts.
The result should be an array with only the selected post.
2
Answers
Please don’t use
get()
function on the model.Btw, the author shouldn’t have a post_id, because author can have multiple posts. you should update the database structure or you are doing wrong in the controller. (my bad, Thanks @techno)
When you are inside the Controller you already have the Post you want. If you call
get
you are getting all posts with a new query.To select specific columns from a relationship, you can do it like this:
Notice that including the
id
is required.To specify columns for the Post, there’s no way to do it per route, the only way is to override how Laravel resolves the implicit binding. For that, you can add this in your
Post
model class:Notice that including the
author_id
is required to load the Author afterwards inside the Controller method. Also keep in mind this will affect all routes where you implicitly load a Post.