我有这种类型的查询,我想将其转换为 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();
关于php - 与 Laravel 一对一聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48812565/