How to get only specific columns in Eloquent using relationships “with()”
Categorized as Laravel
If you have relationships like hasMany
or manyToMany
in Laravel and you need to extract data from specific columns of your DB table with your Eloquent query, just do it like that:
$posts = Post::find(1)
->with('category:id,slug,name')
->get();
Don’t forget to always use a column with primary key
(in the example above it is id
column) and not to put spaces between comma separated values.