How can I change this query [email protected]?
$query = "select * from nozzle a left join nozzlereading b on a.id = b.nozzle
and b.date=" . $date . " where a.meter=" . $meterid;
$reading = DB::select($query);
My models are Nozzle and Nozzlereading. But left join is not working properly.
I tried with code written below. But its not working properly.
$nozzleReadings = Nozzle::select('nozzle.id', 'nozzle.name', 'nozzle.meter', 'nozzlereading.date', 'nozzlereading.reading as myreading')
->leftJoin('nozzlereading', function ($join) use ($date, $meterid) {
$join->on('nozzle.id', '=', 'nozzlereading.nozzle')
->where('nozzlereading.date', '=', $date)
->where('nozzle.meter', '=', $meterid);
})->get();
TIA
2
Answers
Please try this code.
I hope it helps.
You can try specifying the relationships to simplify the Eloquent model queries.
The structure of the output will be slightly different so be aware of the different nested arrays.
You can refer to https://laravel.com/docs/10.x/eloquent-relationships for more inspiration and guidance.