0

I'm making a filter for my search, my car table has a relationship with the color table so when I filter the search with the color I need a car with that color, for the example, I'm just using one filter but in my app, it actually has many filters, so I want to know which perform better to using a whereHas method for each filter like this:

$cars = Car::whereHas('colors', function ($query) use ($request){
    $query->where('name', $request->color);
})->get();

or just add the color name to car table so I can just use

$cars = Car::where('color', $request->color)->get();
Adrian Edy Pratama
  • 3,841
  • 2
  • 9
  • 24
  • does this answer your question? - https://stackoverflow.com/questions/30231862/laravel-eloquent-has-with-wherehas-what-do-they-mean – BillJustin Aug 25 '20 at 02:11
  • Thank you, but no, I already know that what I want to know now is which one perform better now – Adrian Edy Pratama Aug 25 '20 at 02:18
  • 2
    Your situation is querying one table vs querying two tables. In terms of performance, one table would obviously be better. I'm sure that there's more you need to think about performance wise and management wise, but yeah, it is what it is; selecting from one table should be faster than getting results from two or more tables. – user3532758 Aug 25 '20 at 02:19

0 Answers0