我有此路由传递通知模型:
Route::view('masaya', 'pages.notifications-table', [
'data' => App\Notification::all()
]);
如何将查询传递给模型而不是:
$inspect = DB::table('notifications')
->leftJoin('franchises', 'franchises.id', '=', 'notifications.franchise_id')
->leftJoin('units', 'units.id', '=', 'notifications.unit_id')
->select('franchises.id as fid', 'franchises.case_number', 'franchises.authorize_units', 'franchises.deno', 'franchises.route_name', 'franchises.expiry_date', 'franchises.date_granted', 'units.id as uid', 'units.plate_number', 'units.motor_number', 'units.chassis_number', 'units.make', 'notifications.id', 'notifications.remarks', 'notifications.created_at', 'notifications.plate_picture')
->orderBy('units.updated_at', 'desc')
->paginate(20);
答案 0 :(得分:0)
如果这是您经常使用的结果集,则可以在App\Notification
模型中使用静态方法,例如:
public static function withFranchisesAndUnits()
{
return \DB::table('notifications')
->leftJoin('franchises', 'franchises.id', '=', 'notifications.franchise_id')
->leftJoin('units', 'units.id', '=', 'notifications.unit_id')
->select('franchises.id as fid', 'franchises.case_number', 'franchises.authorize_units', 'franchises.deno', 'franchises.route_name', 'franchises.expiry_date', 'franchises.date_granted', 'units.id as uid', 'units.plate_number', 'units.motor_number', 'units.chassis_number', 'units.make', 'notifications.id', 'notifications.remarks', 'notifications.created_at', 'notifications.plate_picture')
->orderBy('units.updated_at', 'desc')
->paginate(20);
}
..并这样称呼它:
Route::view('masaya', 'pages.notifications-table', [
'data' => App\Notification::withFranchisesAndUnits()
]);