php - Laravel Eloquent - Ordering by specific ID of the child -
i have 3 models: schedule, shift, , user. schedule has many shift, , each shift belongs schedule , has 1 user.
to schedule (with shifts , user), use:
schedule::with(['shifts', 'shifts.user'])->find($id); i want order schedule specific user id - i.e. if looking @ schedule, want shift first 1 on schedule.
all find online how order parent eloquent model (i.e. schedule here) using orderbyraw()
user::orderbyraw(db::raw("field(id, $user_id)")); i tried put in $query of with statement, didn't work:
schedule::with( [ 'shifts', 'shifts.user' => function($query) use ($user_id) { $query->orderbyraw(db::raw("field(id, $user_id)")); } ])->find($id); any ideas?
Comments
Post a Comment