将Laravel ORM与()和select()方法结合使用

时间:2017-05-18 09:30:41

标签: php laravel activerecord laravel-5 orm

我有玩家和付款模式。在付款表中有" player_id"柱。 我想选择播放器+付款关系的特定属性。换句话说,我想结合这2个查询。

 1. User::where(/* some condition */)->with('payment')->get();
 2. User::where(/* some condition */)->select([/* some attributes */])->get();

但如果我尝试

User::where(/* some condition */)->select([/* some attributes */])->with('payment')->get();

返回的模型关系数组为空。

2 个答案:

答案 0 :(得分:0)

你可以尝试一下 -

`User::where(/* some condition */)->select([/* some attributes of user */])->with(['payment' => function ($q) {
    return $q->select(/* some attributes payment */)
}])->get();`

答案 1 :(得分:0)

有点棘手,但应该有效

User::where(/* some condition */)->select([/* some attributes */])->get()->load('payment');