与 Laravel 一对一聊天
One on one chat with Laravel
我有这种类型的查询,我想将其转换为 Laravel eloquent 查询,而不必执行行查询或 DB::select
。
这是行查询的样子
Where (user_id = '$from' OR to = '$from') AND (user_id = '$to' OR to = '$to')
如何在 eloquent 中执行此操作?到目前为止我刚刚完成
return $query->where('user_id', $from)->orWhere('to', $from)->get();
目的是在不让其他用户聊天的情况下让两个用户之间进行对话。
你可以这样做:
return $query->where(function($q) use ($from) {
$q->where('user_id', $from)
->orWhere('to', $from);
})->where(function ($q) use ($to) {
$q->where('user_id', $to)
->orWhere(to, $to);
})->get();
感谢@Laerte
我从你的代码中找到了更好的方法和更清晰的方法
return $query->where(['user_id' => $from, 'to' => $from])->orWhere(['user_id' => $to, 'to' => $to])->get();
我有这种类型的查询,我想将其转换为 Laravel eloquent 查询,而不必执行行查询或 DB::select
。
这是行查询的样子
Where (user_id = '$from' OR to = '$from') AND (user_id = '$to' OR to = '$to')
如何在 eloquent 中执行此操作?到目前为止我刚刚完成
return $query->where('user_id', $from)->orWhere('to', $from)->get();
目的是在不让其他用户聊天的情况下让两个用户之间进行对话。
你可以这样做:
return $query->where(function($q) use ($from) {
$q->where('user_id', $from)
->orWhere('to', $from);
})->where(function ($q) use ($to) {
$q->where('user_id', $to)
->orWhere(to, $to);
})->get();
感谢@Laerte
我从你的代码中找到了更好的方法和更清晰的方法
return $query->where(['user_id' => $from, 'to' => $from])->orWhere(['user_id' => $to, 'to' => $to])->get();