RaedN
RaedN

Reputation: 43

How can I select a specific column from the first table in a with relation in laravel?

enter image description here

For example, I want to select id, title from books instead of *, is there a way to do that?

Upvotes: 0

Views: 37

Answers (2)

Andreas Hunter
Andreas Hunter

Reputation: 5024

Here is example:

$books = App\Book::select('id', 'title')->with('author')->get();

Upvotes: 0

Rouhollah Mazarei
Rouhollah Mazarei

Reputation: 4153

If you are using a relation, try this:

$book->author()->get(['id', 'title']);

The above query give you the id and title of the author.

For simple queries, you can omit the relation:

$book->get(['id', 'title']);

This will return an array containing the id and title of the selected book.

Upvotes: 2

Related Questions