Great question! I forgot about this...anyway the docs suggest this way..
$books = Book::with('author:id,name,book_id')->get();
https://laravel.com/docs/10.x/eloquent-relationships#constraining-eager-loads
Hi There,
A simple question, is it possible to use with() to return a string instead of a object, and if not what would be the best approach?
$user = User::where('email', $request->email)->with('country')->first();
Would return
"data": { "id": 1, "email": "[email protected]", "country": { "id": 1, "name": "Afghanistan" }, },
And I want
"data": { "id": 1, "email": "[email protected]", "country":"Afghanistan", },
Thanks.
If its for sending as an API response, the usual way would be to create an API response https://laravel.com/docs/10.x/eloquent-resources#main-content and then map just the fields you want from the parent record and any nested relations.
Please or to participate in this conversation.