Eager loading a relationship with parent select statement

Posted 3 years ago by cullymason

So I have a Ticket model which belongs to a Project model. In the end, I want to select Tickets with certain columns included as well as the project id for each in a collection.

So this works:

    $results = Ticket::get();
    $results->load(['project'=> function($query){
        $query->lists('id');
    }]);
   

Returns:

[{"id":1,"project_id":8,"inoc_id":"31-722761","rfo_code_id":2,"status":"0","customer_affected":0,"severity":"2","notes":"Fuga unde est labore autem tempore beatae iure.","verified":1,"owner":6,"created_at":"2015-10-19 13:46:20","updated_at":"2015-10-19 13:46:20","project":{"id":8}}]

But as soon as I try to select certain columns for a Ticket, the project always comes back null.

    $results = Ticket::select('notes')->get();
    $results->load(['project'=> function($query){
        $query->lists('id');
    }]);
    return $results;
    

[{"notes":"Fuga unde est labore autem tempore beatae iure.","project":null},{"notes":"Enim quia eos dolorum ut.","project":null}]

Any ideas?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.