当前位置: 动力学知识库 > 问答 > 编程问答 >

Laravel Simple Nested Select Statement with Relationship

问题描述:

I have a mySql query that I need to convert using Eloquent(recommended) if possible.

Here is my query:

SELECT * FROM

( SELECT `uid`, `revision` FROM `tasks` ORDER BY `revision` DESC)

x GROUP by uid

In my Project model I have the following:

$this->hasMany('\App\Task', 'project_id');

And now, How do I convert the sql query above using eloquent.

Any help is appreciated.

Cheers all.

网友答案:

I found the solution. There is DB::select in laravel which is essential in this case.
Here is the solution:

$sub = 'SELECT * FROM `tasks` ORDER BY `revision` DESC';
                $results = DB::select(
                        DB::raw('select * from (' . $sub . ') x GROUP by uid')
                );
分享给朋友:
您可能感兴趣的文章:
随机阅读: