1

Edit

This question is unique because it poses unique problems such as:

  1. relations within the with components. (items.product.stockManagement).
  2. A large amount of components, this causes the accepted answers of the linked question to not apply.

Suppose you have a large with() like the following:

$order = Order::with([
        'company',
        'complaints',
        'person',
        'items',
        'items.product',
        'items.product.stockManagement',
        'status', 
    ])->findOrFail($id);

How can I then select with all their relations but specific columns for some of them?

$order = Order::with([
        'company',   // select only id,name, size
        'complaints',  // select only id, name , body
        'person',     
        'items',  
        'items.product',  // select only id, name , price
        'items.product.stockManagement',
        'status',  // select only id
        'items.product.media',
        'items.product.mainProduct',
        'items.product.mainProduct.media'
    ])->findOrFail($id);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mister Verleg
  • 4,053
  • 5
  • 43
  • 68
  • 1
    Does this answer your question? [Get Specific Columns Using “With()” Function in Laravel Eloquent](https://stackoverflow.com/questions/19852927/get-specific-columns-using-with-function-in-laravel-eloquent) – Douwe de Haan Jun 25 '20 at 09:32
  • 1
    Could be that this is a duplicate, but the other question has a lot of not helpful answers, but maybe that post could be helpful as well. – Douwe de Haan Jun 25 '20 at 09:33
  • @DouwedeHaan Thank you, the question you linked seems to be asking the same thing, but here the most voted awnser gives a clear and precicse awnser, unlike the most voted comments on the other question – Mister Verleg Jun 25 '20 at 09:49

4 Answers4

7

Like this:

$order = Order::with([
    'company:id,name,size',
    'complaints:id,name,body',
    'person',     
    'items',  
    'items.product:id,name,price',
    'items.product.stockManagement',
    'status:id',
    'items.product.media',
    'items.product.mainProduct',
    'items.product.mainProduct.media'
])->findOrFail($id);

The documentation is very brief about the loading of specific columns (you even have to scroll down a bit to the heading that says "Eager Loading Specific Columns").

You may not always need every column from the relationships you are retrieving. For this reason, Eloquent allows you to specify which columns of the relationship you would like to retrieve:

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

Note:

When using this feature, you should always include the id column and any relevant foreign key columns in the list of columns you wish to retrieve.

Douwe de Haan
  • 6,247
  • 1
  • 30
  • 45
3

You can also provide a callback for some more advanced relation querying.

Order::with([
    'company' => function ($q) {
         $q->select('id', 'name');
    }
])
Dino Numić
  • 1,414
  • 2
  • 9
  • 17
2

I faced the same problem. You need to specify the foreign id not id (primary key).

For example:

Data::with('other:id,name')->get();

It won't be working if you customize the foreign name. So, you need to add your foreign column name completely.

Data::with('other:foreign_id,name')->get();

And that will work!

Showrin Barua
  • 1,737
  • 1
  • 11
  • 22
0
$order = Order::with(
['company' => function($query) {
    $query->select('id','name','size')
 }),
'complaints' => function($query) {
    $query->select('id','name','body')
 }),
'person',    
'items',  
'items.product'  => function($query) {
    $query->select('id','name','price')
 }), 'items.product.stockManagement','status'=> function($query) {
    $query->select('id')
 }),'items.product.media', 'items.product.mainProduct', 'items.product.mainProduct.media'])->findOrFail($id);