我如何在Laravel Eloquent中编写这个MySQL查询?

时间:2015-07-28 17:53:45

标签: php mysql laravel eloquent relationships

我正在尝试使用半复杂的查询(无论如何,按照我的标准)但我无法想到如何使用查询构建器或使用雄辩的关系来完成它。

我的关系表为:user_id_1 | user_id_2 | status | action_user_id如果是朋友,action_user_id可以忽略,status = 1。每个关系只有一行,因此如果要选择属于用户17的所有关系,则需要同时检查user_id_1和user_id_2,因为id可以位于任一列中。我从这里跟踪了数据库结构:http://www.codedodle.com/2015/03/social-network-friends-relationship.html

我正在尝试执行的查询类型是:

SELECT users.*
FROM users
LEFT JOIN users_relationships AS r
    ON (
        users.id = r.user_id_1
        AND r.user_id_1 != $user_id
    ) OR (
        users.id = r.user_id_2
        AND r.user_id_2 != $user_id
    )
WHERE r.status = 1
    AND (
        r.user_id_1 = $user_id
        OR r.user_id_2 = $user_id
    )

这样的事情在一个雄辩的关系中是最好的,所以它很容易执行:$user->friends它会返回上面的结果,但我被困在各个方向。

遗憾的是,我最好的尝试是不使用关系:

User::with('profile')->join('user_relationships as r', function($join) use ($user_id) {
    $join->on(DB::raw('( users.id = r.user_id_1 AND r.user_id_1 != ? )', [$user_id]), DB::raw(''), DB::raw(''));
    $join->orOn(DB::raw('( users.id = r.user_id_2 AND r.user_id_2 != ? )', [$user_id]), DB::raw(''), DB::raw(''));
})
->where('r.status', 1)
->where(function($query) use ($user_id) {
    $query->where('r.user_id_1', $user_id)
        ->orWhere('r.user_id_2', $user_id);
})
->get(['users.*']);

然而,这给了我与参数有关的错误:

SQLSTATE[HY093]: Invalid parameter number (SQL: select `users`.* from `users` inner join `user_relationships` as `r` on ( users.id = r.user_id_1 AND r.user_id_1 != 1 ) or ( users.id = r.user_id_2 AND r.user_id_2 != 30 ) where `users`.`deleted_at` is null and `r`.`status` = 30 and (`r`.`user_id_1` = ? or `r`.`user_id_2` = ?))

我不确定如何做我想做的事。

2 个答案:

答案 0 :(得分:2)

问题在于:

  

DB::raw('( users.id = r.user_id_1 AND r.user_id_1 != ? )', [$user_id])

DB :: raw()不会为绑定采用第二个参数。请改用->on()->where()(感谢user4621032):

->leftJoin('users_relationships AS r', function($join) use($user_id) {
    $join->on('users.id','=','r.users_id_1')->where('r.users_id_1','!=',$user_id)
         ->orOn('users_id','=','r.users_id_2')->where('r.users_id_2','!=',$user_id);
})

因此,要根据需要设置->friends()功能,您可以使用:

public function friends()
{
    $user_id = $this->id;
    return self::with('profile')
        ->leftJoin('users_relationships AS r', function($join) use($user_id) {
            $join->on('users.id','=','r.users_id_1')->where('r.users_id_1','!=',$user_id)
                 ->orOn('users_id','=','r.users_id_2')->where('r.users_id_2','!=',$user_id);
        })
        ->where('r.status', 1)
        ->where(function($query) use ($user_id) {
            $query->where('r.user_id_1', $user_id)
            ->orWhere('r.user_id_2', $user_id);
        })
        ->get(['users.*']);
}

答案 1 :(得分:1)

可能是这样的

 DB::table('users')
            ->leftJoin('users_relationships AS r', function($join) use($user_id){
                $join->on('users.id','=','r.users_id_1')->where('r.users_id_1','!=',$user_id)
                ->orOn('users_id','=','r.users_id_2')->where('r.users_id_2','!=',$user_id);
            })
              ->where('r.status',1)
              ->where('r.user_id_1','=',$user_id)
              ->orWhere('r.user_id_2','=',$user_id)
              ->get();