Is there a way to select fields from an eager loaded table in Laravel?

后端 未结 2 1816
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-24 12:55

So I have a couple of relational tables defined below

Parent DB:

public function backorderQuantities(){
        return $this->hasMany(BackorderQuantity:         


        
相关标签:
2条回答
  • 2021-01-24 12:55

    you can select columns of eger loading by using with :

     Item::with('backorderQuantities:QUANTITY,...');
    

    don't forgot pass forigen key of backorderQuantities table to selected columns

    Edit :

    for sorting by child column you should use join statement :

     Item::select('items.*')->leftJoin('items.ITEMNMBR','=','backorderQuantities.ITEMNMBR')->groupBy('backorderQuantities.ITEMNMBR')->orderBy('backorderQuantities.backorderQuantities')
    
    0 讨论(0)
  • 2021-01-24 13:10

    You will need to use join instead of eager loading.

    Similar question already exists on the Laracasts forum: here

    0 讨论(0)
提交回复
热议问题