Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ozan's avatar
Level 32

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?

0 likes
17 replies
RachidLaasri's avatar
Level 41
$tickets = Ticket::with(array('subject' => function($query)
{
    $query->select('name');

}))->get();
6 likes
JarekTkaczyk's avatar

@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.

50 likes
dimsav's avatar

Has anyone tried this in laravel 5.1? Doesn't seem to work.

tranghaviet's avatar

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();
4 likes
hendra1's avatar

In laravel 5.6

$users = App\Book::with('author:id,name')->get();

3 likes
normpetroff@gmail.com's avatar

@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()

1 like
stuartcusack's avatar

@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.

3 likes
Neeraj1005's avatar

@stuartcusack

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.

1 like
DanielVZA's avatar

@Neeraj1005 try adding the foreign key 'subject_id' inside the get (). Ticket::with('subject:ticket_id,name')->get(['id','created_at','subject_id']);

3 likes

Please or to participate in this conversation.