在laravel 5中编写自定义查询

时间:2016-02-15 05:43:24

标签: php laravel-5

我希望得到一些数据,并且还要将员工姓名与雇员的最后休假日期联系起来 所以我写了这个查询 我怎么能在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

3 个答案:

答案 0 :(得分:4)

$empls= DB::table('employee')
            ->join('hr_leave', 'hr_leave.hrid', '=', 'employee.id')
            ->select('employee.*')
            ->where('hr_leave.oid', $id)
            ->get();

更多信息 - https://laravel.com/docs/5.1/queries

答案 1 :(得分:4)

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

答案 2 :(得分:0)

您可以使用以下示例。

$ users = \ DB :: select(“SELECT users。,products。 FROM users LEFT JOIN products ON用户。id =产品。 user_id WHERE 1 ORDER BY用户。id ASC“);