我有这个代码:$from = $data->chat_from_user;$to = $data->chat_to_user;\Log::info($from); // 1\Log::info($to); // 2$result = DB::connection('mysql_live')->table('user_chatmessages') ->where(function ($query) use ($from, $to) { $query->where('from_user', $from)->where('to_user', $to); })->orWhere(function ($query) { $query->where('from_user', $to)->where('to_user', $from); })->orderBy('date_added', 'asc')->get();我收到一个错误:Undefined variable: to。我找到了很多主题,但解决方法始终是使用use(). 但它仍然告诉我变量没有定义。例如,这完美地工作:$result = DB::connection('mysql_live')->table('user_chatmessages') ->where(function ($query) { $query->where('from_user', '1')->where('to_user', '2'); })->orWhere(function ($query) { $query->where('from_user', '2')->where('to_user', '1'); })->orderBy('date_added', 'asc')->get();
1 回答
人到中年有点甜
TA贡献1895条经验 获得超7个赞
有两个关闭,你错过了一个。查看orWhere
$result = DB::connection('mysql_live')->table('user_chatmessages')
->where(function ($query) use ($from, $to) {
$query->where('from_user', $from)->where('to_user', $to);
})->orWhere(function ($query) use ($from, $to){ // <- here
$query->where('from_user', $to)->where('to_user', $from);
})->orderBy('date_added', 'asc')->get();
- 1 回答
- 0 关注
- 145 浏览
添加回答
举报
0/150
提交
取消