Query Post Model With Specific Column
To only query the "id" and "username" columns of the relationship model, you can write the code below.
<?php Post::query() ->with(['user' => function($query) { $query->select(['id', 'username']); }]) ->get();
Using the Load method
If you are using the "load" method then you can write it as follows.
<?php $post = Post::first(); $post->load(['user' => function($query) { $query->select(['id', 'username']); }]);
Leave a reply