我有這個代碼:$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 關注
- 165 瀏覽
添加回答
舉報
0/150
提交
取消