Select custom columns from Laravel belongsToMany relation

后端 未结 3 1754
悲哀的现实
悲哀的现实 2021-01-13 05:06

Im trying to select only specific attributes on the many-to-many relation users, just like in one-to-one. But using select() on belongsToMany

3条回答
  •  粉色の甜心
    2021-01-13 05:45

    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',...));
    

提交回复
热议问题