我正在使用 Laravel 開發一個小項目,我盡力理解 Laravel 文檔以創建一個不錯的解決方案,但是這次我發現一些難以理解 WHEN 子句有人可以通過給我一個例子來向我解釋這段代碼: $role = $request->input('role');$users = DB::table('users') ->when($role, function ($query, $role) { return $query->where('role_id', $role); }) ->get();文檔: https: //laravel.com/docs/7.x/queries#conditional-clauses我還想知道第二個參數代表什么[] in the input functionrequest()->input('field', []);
1 回答

繁花如伊
TA貢獻2012條經驗 獲得超12個贊
只要變量 role 等于 true,該查詢就會執行:
return $query->where('role_id', $role);
關于第二個問題,如果請求參數“字段”未填寫,則默認值設置將為:[]。
另一個例子:
$foo = $request->input('test', 69);
如果請求參數test不填,則參數foo將為69。
- 1 回答
- 0 關注
- 109 瀏覽
添加回答
舉報
0/150
提交
取消