简体   繁体   中英

Laravel with([]) select only some columns of the relation

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);

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.

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

Order::with([
    'company' => function ($q) {
         $q->select('id', 'name');
    }
])
$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);

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM