我有两个数据库表,items
并且measurement_units
- 项目有测量单位。
现在的问题是我要选择从特定的列items
和一些列measurement_unit
。我想使用Eager loading
例如
$items_with_mu = Item::with("measurement_unit")->select(["item_name", "item_stock"])->first();
访问measurement_unit 时。它返回null。如果没有选择函数,它会返回数据(measurement_unit)。
$items_with_mu->measurement_unit;
任何人都可以帮助我并为我的英语感到抱歉。
试试这个
Item::with(['measurement_unit' => function($q) {
$q->select('id','unit_column'); //specified measurement_unit column
}])
->select('id','measurement_unit_id','item_name')
->get();
如果你的 Laravel 版本 >=5.5 那么你可以写一行
Item::with('measurement_unit:id,unit_column')
->select('id','measurement_unit_id','item_name')
->get()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句