Laravel Eager Loading - Load only specific columns

前端 未结 7 1422
梦毁少年i
梦毁少年i 2020-12-05 00:35

I am trying to eager load a model in laravel but only return certain columns. I do not want the whole eager loaded table being presented.

public function car         


        
7条回答
  •  离开以前
    2020-12-05 00:40

    In your controller you should be doing something like

    App\Car::with('owner:id,name,email')->get();
    

    Supposing that you have two models defined like below

    belongsTo('App\Owner', 'owner_id');
        }
    }
    

    and

    hasMany('App\Car', 'owner_id');
        }
    }
    

    and you have two tables something like:

    owners: id | name | email | phone | other_columns...
    

    and

    cars: id | owner_id | make | color | other_columns...
    

    Credits go to the docs: eloquent-relationships#eager-loading scroll to Eager Loading Specific Columns

提交回复
热议问题