S
S
suhuxa12017-09-03 11:31:22
Laravel
suhuxa1, 2017-09-03 11:31:22

How to write such a query using laravel eloquent?

There is this request:

SELECT auto.*, count(parts.id) as total
FROM auto
LEFT JOIN parts ON auto.id = parts.auto_id
GROUP BY auto.id

Can such a query be adapted to laravel eloquent?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita Dergachov, 2017-09-03
@suhuxa1

DB::table('auto')
                     ->select('auto.*', DB::raw('count(parts.id) as total'))
                     ->join('parts', 'auto.id', '=', 'parts.auto_id')
                     ->groupBy('auto.id')
                     ->get();

U
UksusoFF, 2017-09-03
@UksusoFF

App\Auto::withCount('parts')->get()
https://laravel.com/docs/5.4/eloquent-relationship...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question