I have following scope in my eloquent model and I want to add two conditions to it. I need help in doing it.
public function scopeImages($query) {
$query->join('images as i', function ($join) {
$join->on('i.vessel_id', '=', 'vessel.id')
->where('i.sort', '=', 'min(i.sort)');
})
->leftjoin('users', 'users.id', '=', 'vessel.user_id')
->select('vessel.*', 'i.image as image', 'users.name as brokername');
}
images
table has featured
and sort
columns. I want to select one row where images.featured is 1
and min sort
of the returned results. If there are no images.featured=1
then I want to select min of sort
.
Currently the above scope selects image of min sort
for each vessel_id