$tickets = Ticket::with(array('subject' => function($query)
{
$query->select('name');
}))->get();
Eloquent eager loading specific columns
I have a Ticket model which and a belongsTo relationship for ticket subject.
Ticket::with('subject')->get();
this also takes the subject id. I only want subject name. How can i do that?
@OzanKurt There is one thing to have in mind when selecting only particular columns on the related table: You must always select the foreign key and primary key of the relation.
So in your case you need to select the id of Subject (and obviously subject_id on the Ticket model):
Ticket::with(['subject' => function($q)
{
$q->select('name', 'id');
}])->get();
Otherwise you would get null for each related subject, because Eloquent wouldn't be able to match the parent with child of the relation.
That said, you cannot do this with eager loading. You can achieve exactly what you need with joins:
Ticket::leftJoin('subjects', 'subjects.id', '=', 'tickets.subject_id')->select('tickets.*', 'subjects.name as subject_name')->get();
If you want only tickets with subjects, then use join instead of leftJoin.
@JarekTkaczyk Thanks! For the primary key and foreign key thing :)
@JarekTkaczyk You should get the correct answer :)
Has anyone tried this in laravel 5.1? Doesn't seem to work.
@dimsav the best answer on this thread is wrong, read @JarekTkaczyk one.
@JarekTkaczyk your answer is the correct one!
@Ozan If possible, please update the "Best Answer" to @JarekTkaczyk 's answer, since it is more complete.
Nop, he didn't even finish the book he told he's writing: https://leanpub.com/eloquence-emerging
We also can do as @RachidLaasri answer for Nested Eager Loading, for example:
$bookings = Booking::with([
'car' => function ($q) {
$q->select('id', 'driver_id');
$q->with(['driver' => function ($q) {
$q->select('id');
}]);
}])->all();
In laravel 5.6
$users = App\Book::with('author:id,name')->get();
@hendra1 This only works on the first relationship.. If you try to do this when there is more then one relationship, it fails.
$books = Book::with('author.company:id,name)->get()
@JarekTkaczyk answer works!
I also needed the ability to select only certain fields from the 'Ticket' class (using Jarek's example). For this you need to include BOTH the foreign key in the eager loaded column array AND the local primary key in the base class column array. (e.g. ticket_id AND id)
I also came across a cleaner alternative method for selecting specific fields in the eager loaded models.
So altogether I have something like:
Ticket::with('subject:ticket_id,name')->get([ 'id', 'created_at']);
With this I can get just the 'name' column for the Subject models and the and 'created_at' column for the Ticket models.
@stuartcusack What type of relationship is subject?
@jarektkaczyk, Legend!
Ticket::with('subject:ticket_id,name')->get([ 'id', 'created_at']);
In this scenario, you will get subject null like this
{
"id": 1,
"created_at": your_data,
"subject": null, #you will get the null eager load data
}
Let me know If I'm wrong. Because I tried this.
@Neeraj1005 try adding the foreign key 'subject_id' inside the get (). Ticket::with('subject:ticket_id,name')->get(['id','created_at','subject_id']);
Please or to participate in this conversation.