samedi 9 janvier 2016

laravel time fields return null when left joined

table#1 [person]:

increment('id');
string('name',20);

table#2 work_hours:

increment('id');
integer('person_id',10);
date('report_date');
time('working_hours');

Query:

select person.id, person.name,report_date,working_hours
from person
left join work_hours as hours
on hours.person_id = person.id
and report_date = 'yyyy-mm-dd'

Query Result: lists all people with working_hours of the report_date with null where record does not exist - This is required result.

Eloquent:

$result=Person::select('person.id','person.name','report_date','working_hours')
    ->leftJoin('work_hours',function($join){
        $join->on('person.id','=','work_hours.person_id')
             ->where('report_date','=','yyyy-mm-dd');
    })->get();

Eloquent Result: lists all people with null values for report_date and working_hours in all records!!

How can I overcome this problem situation?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire