Write Custom query in laravel 5

2019-05-13 00:42发布

I want to get the some data and also last leave date of the employee with their employee name so i wrote this query how can i do this in laravel 5

SELECT employee.firstname,employee.lastname,employee.id,hr_leave.*,LAST(hr_leave.date) AS lastleaveday
FROM employee
INNER JOIN hr_leave
ON employee.id=hr_leave.hrid
WHERE hr_leave.oid=$id

employee
id  oid  firstname  lastname

id is primary key

hr_leave
id  oid  hrid  leave_from  leave_to  days  reason

id is primary key hrid is the foreign key

标签: php laravel-5
3条回答
爷的心禁止访问
2楼-- · 2019-05-13 00:52
$empls= DB::table('employee')
            ->join('hr_leave', 'hr_leave.hrid', '=', 'employee.id')
            ->select('employee.*')
            ->where('hr_leave.oid', $id)
            ->get();

more info - https://laravel.com/docs/5.1/queries

查看更多
Root(大扎)
3楼-- · 2019-05-13 01:10
For Select --> DB::select('your query here.....');
For Insert --> DB::insert('your query here.....');
For Update --> DB::update('your query here.....');
For Delete --> DB::delete('your query here.....');
For General -->  DB::statement( 'your query here....' );

Read this Article

查看更多
再贱就再见
4楼-- · 2019-05-13 01:10

You can use below example.

$users = \DB::select(“SELECT users.,products. FROM users LEFT JOIN products ON users.id = products.user_id WHERE 1 ORDER BY users.id ASC”);

查看更多
登录 后发表回答