-1

I have 4 tables (Cars, Vignettes, Inspections and Insurances). Vignettes, Inspections and Insurances are "belongsTo" to Cars.

I need to combine results of these three tables into one query and sorting by 'created_at' and take first 3-4 of them. How can I achieve that?

All of the three tables have same column names and types

KJELYAZKOV
  • 13
  • 4

1 Answers1

0

You should be able to chain things together, something like

$car->with('Vignettes')
 ->orderBy('created_at', 'desc)
 ->take(4)
 ->get();

As with everything laravel, the docs are your friend.

damask
  • 529
  • 4
  • 17
  • I tried this: Car::find(1)->with('vignettes', 'insurances', 'inspections') ->orderBy('expire_on', 'desc') ->take(4) ->get(); But doesnt work properly. – KJELYAZKOV Mar 11 '21 at 22:32
  • Maybe I need a pivot table or just add all of three tables into one with field ' tax_type ' – KJELYAZKOV Mar 11 '21 at 22:34