Not sure what it is you want here, but the where should be before the orderBy.
Nov 6, 2022
9
Level 3
Help with MIN in query
I am trying to figure out how to get the records with the lowest value in a db column in a join query.
Part of the query is
// GET THE DEFAULT IMAGE IN THE JOIN TABLE
->join('gallery_photos as gp', function ($join) {
$join->on('gp.gallery_id', '=', 'gallery.id')
->orderBy('gp.sort', 'asc')
->where('gp.sort', '=', 1);
})
// GET THE DEFAULT FALLBACK IMAGE RECORD
->join('photos', function ($join) {
$join->on('photos.id', '=', 'gp.photo_id');
})
The part where gp.sort = 1 is where I am stuck. Perhaps during the user sorting or deleting there is no sort = 1 so I need to get the lowest possible sort value.
Thanks
Please or to participate in this conversation.