Select specific columns with Laravel Eloquent To get all of the columns from the users table, we would use the following: $user = User::where('username', 'bobbyiliev')->get(); However, if you wanted to get only a specific column, you could pass it as an argument to the get() method.
You can use Table::select ('name', 'surname')->where ('id', 1)->get () . Keep in mind that when selecting for only certain fields, you will have to make another query if you end up accessing those other fields later in the request (that may be obvious, just wanted to include that caveat).
with() is generally used with eager loading, which is a quick way to pull related models. Basically, it means that, along with the main model, Laravel will preload the listed relationship(s). This is beneficial when you need to load additional data and want to avoid making N+1 DB bad practices.
You can get all the column names from a table using the DB facade and Schema facade. You have to call the getColumnListing() method (using DB and Schema facade) by passing the table name as an argument to get all columns from the table.
Well I found the solution. It can be done one by passing a closure
function in with()
as second index of array like
Post::query()
->with(array('user' => function($query) {
$query->select('id','username');
}))
->get();
It will only select id
and username
from other table. I hope this will help others.
Remember that the primary key (id in this case) needs to be the first param in the
$query->select()
to actually retrieve the necessary results.*
You can do it like this since Laravel 5.5:
Post::with('user:id,username')->get();
Care for the id
field and foreign keys
as stated in the docs:
When using this feature, you should always include the id column and any relevant foreign key columns in the list of columns you wish to retrieve.
For example, if the user belongs to a team and has a team_id
as a foreign key column, then $post->user->team
is empty if you don't specifiy team_id
Post::with('user:id,username,team_id')->get();
Also, if the user belongs to the post (i.e. there is a column post_id
in the users
table), then you need to specify it like this:
Post::with('user:id,username,post_id')->get();
Otherwise $post->user
will be empty.
For loading models with specific column, though not eager loading, you could:
In your Post
model
public function user()
{
return $this->belongsTo('User')->select(['id', 'username']);
}
Original credit goes to Laravel Eager Loading - Load only specific columns
When going the other way (hasMany):
User::with(array('post'=>function($query){
$query->select('id','user_id');
}))->get();
Don't forget to include the foreign key (assuming it is user_id in this example) to resolve the relationship, otherwise you'll get zero results for your relation.
In Laravel 5.7 you can call specific field like this
$users = App\Book::with('author:id,name')->get();
It is important to add foreign_key
field in the selection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With