Select custom columns from Laravel belongsToMany relation

我只是一个虾纸丫 提交于 2019-12-01 06:52:44
Mark

Yes, you actually can.

Computer::with("users")->get(array('column_name1','column_name2',...));

Be careful though if you have the same column name for both tables linked by your pivot table. In this case, you need to specify the table name in dot notation, tableName.columnName. For example if both users and computer has a column name id, you need to do :

Computer::with("users")->get(array('users.id','column_name2',...));

According to Taylor Otwell it is not currently possible: https://github.com/laravel/laravel/issues/2679

I have tried to use a lists('user.email') at the end of the query but I can't make it work.

Computer::with(["users" => function($query){
$query->select('column1','column2','...');
}])->get();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!