Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kshitizmittal's avatar

Convert Custom Query in Laravel

select * from employeedetails as ed LEFT JOIN (SELECT * FROM `punchreports` where date="2018-12-03") as pr on ed.emp_id = pr.emp_id where ed.status="Active"

Can I convert this query in Eloquent ?

or

How to execute this query in Laravel Controller?

0 likes
1 reply
Palak27's avatar

@kshitizmittal

you have to make two Eloquent model one is Employee and second is Reports

in employee model define the relationship with report

if employee has many reports than relationship will be

public function punchreports()
    {
        return $this->hasMany('App\Reports');
    }

and query will be

Employee::with([    
    'punchreports' => function($query) {
          $query->where('date',"2018-12-03)
    }

])->where('status',"Active")->get();

1 like

Please or to participate in this conversation.