Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

imaxim287's avatar

Laravel query using whereHas

I have 2 models: car painted color, and cars

public function color()
{
    return $this->hasOne(Colors::class)->latest();
}

I need to search the latest painted car color but with whereHas, it's searching all colors

$colors = ['black', 'white'];
$cars->whereHas('color', function($cars) use ($colors) {
          return $cars->whereIn('paint_color', $colors);
});

How can I make whereIn to only query on the latest row?

0 likes
15 replies
tykus's avatar

Is paint_color on the Color class or on the Car class?

imaxim287's avatar

On the color class: relation

public function color()
{
    return $this->hasOne(Colors::class)->latest();
}
tykus's avatar

So a Color has a car_id column? How is this not a belongsToMany relationship?

imaxim287's avatar

Yes. Relation color->car:

public function cars()
{
      return $this->belongsTo(Cars::class);
}

But i need get only last row, and whereIn on this row

tykus's avatar

Okay 🤷‍♂️

In that case, you need to have a read of this excellent article by Jonathan Reinink which wxplains (at the end) why your current approach will not work.

In your case, the current color would be the following:

public function color()
{
	return $this->belongsTo(Color::class);
}

public function scopeWithColor($query)
{
	$query->addSelect(['color_id' => Color::select('id')
		->whereColumn('car_id', 'cars.id')
		->latest()
		->take(1)
	]);
}

This approach inverts the relationship, placing a pseudo-column on Car (color_id) and a BelongsTo relationship on the Car class. Now, you query will be:

$colors = ['black', 'white'];
$cars->whereHas('color', function($builder) use ($colors) {
          return $builder->whereIn('paint_color', $colors);
});
imaxim287's avatar

Model color have car_id attribute (its like relatationship table), but car haven't color_id

tykus's avatar

Read my post...

This approach inverts the relationship, placing a pseudo-column on Car (color_id) and a BelongsTo relationship on the Car class.

imaxim287's avatar

But i have error Undefined column: 7 ERROR: column cars color_id

imaxim287's avatar

I have many cars and many colors at the db, i use pagination at the last step $cars->paginate(10)->appends(....);

tykus's avatar

I thought you might read the article I shared, and would understand that you must use the query scope to dynamically add the color_id column to the Car model.:

$cars = Car::withColor()->whereHas('color', function($builder) use ($colors) {
          return $builder->whereIn('paint_color', $colors);
})->get();
imaxim287's avatar

Yes, i read that

$cars = Car::query();
............
............
if($colors) {
	$cars = $cars->withColor()->whereHas('color', function($builder) use ($colors) {
          return $builder->whereIn('paint_color', $colors);
	});
}
$cars->paginate(10)->appends(....);
tykus's avatar

And what is the issue, it should be different to the missing color_id property at the very least?

imaxim287's avatar

Undefined column: 7 ERROR: column cars color_id at >whereHas('color'

tykus's avatar

That should not be that case if you have used the withColor scope; clearly this scope adds a color_id colum/property to the Car model.

imaxim287's avatar

But i have error, i use this code

$cars = Car::query();
............
............
if($colors) {
	$cars = $cars->withColor()->whereHas('color', function($builder) use ($colors) {
          return $builder->whereIn('paint_color', $colors);
	});
}
$cars->paginate(10)->appends(....);

Please or to participate in this conversation.