Try
App\Batting::whereId(72548)->with('player:id,firstName,lastName')->get();
See "Eager Loading Specific Columns": https://laravel.com/docs/5.6/eloquent-relationships#eager-loading
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hey Gang
I am hitting my head against the wall trying to figure out how to convert this simple SQL statement to a relevant Eloquent query
SELECT
players.nameFirst,
players.nameLast,
batting.*
FROM batting
INNER JOIN players ON batting.playerID = players.playerID
WHERE batting.id = 72548;
I can get the data I want using
App\Batting::whereId(72548)->with('player')->get();
But can't get it to return a single (flat object) with just the nameFirst and nameLast fields from the players table.
I still need to flatten the result object
So you have a collection now after the query runs, so you can use those methods on the result. https://laravel.com/docs/5.6/eloquent-collections#available-methods
The collapse one might be the one you want.
Please or to participate in this conversation.