這是一個默認的 eloquent 條件:
if($request->has("parent")) => {
Specializatio::where('id', $request->id)
->when($request->parentid, function($query) use ($request){
return $query->where('parent_id', $request->parent_id);
})
}
它迅速成長為基于條件.when的請求物件的長串列。是否有任何靈活的機制來管理它,也許在另一個類中,助手?
uj5u.com熱心網友回復:
也許查詢范圍和速記函式會讓你的代碼更簡潔。
class Specialization extends Model
{
public function scopeParent($query, $parentId)
{
return $query->where('parent_id', $parentId);
}
}
Specialization::whereKey($request->id)
->when($request->filled('parent_id'), fn ($query) => $query->parent($request->parent_id));
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/514636.html
標籤:拉拉维尔
上一篇:試圖顯示當前登錄的用戶個人資料。我做錯了什么|拉拉維爾9
下一篇:在laravel9中獲取http://127.0.0.1:8000/route('welcome')而不是http://127.0.0.1:8000/
