欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Laravel数据库操作的三种方式 最大值 最小值 数量 个数 数目等 有大用 有大大用

How do I accomplish this in Laravel 4.1 QUery Builder?

select * from orders where id = (select max(`id`) from orders)

I tried this,working but can't get the eloquent feature.

DB::select(DB::raw('select * from orders where id = (select max(`id`) from orders)'));

Any idea to make it better?

shareimprove this questionedited Apr 15 '14 at 1:48asked Apr 15 '14 at 1:36Shiro2,40642450


5 Answers 正确答案   

$min_project_id = Operation::where('kehu_id',$id)->min('project_id'); 这样子好像是可以的

DB::table('orders')->max('id'); 这样子肯定是可以的

 





 

You should be able to perform a select on the orders table, using a raw WHERE to find the max(id) in a subquery, like this:

 DB::table('orders')->where('id', DB::raw("(select max(`id`) from orders)"))->get();


If you want to use Eloquent (for example, so you can convert your response to an object) you will want to use whereRaw, because some functions such as toJSON or toArray will not work without using Eloquent models.

 $order = Orders::whereRaw('id = (select max(`id`) from orders)')->get();

That ofcourse requires that you have a model that extends Eloquent.

 class Orders extends Eloquent {}

As mentioned in the comments, you don't need to use whereRaw, you can do the entire query using the query builder without raw SQL.

 // Using the Query Builder
 DB::table('orders')->find(DB::table('orders')->max('id'));

 // Using Eloquent
 $order = Orders::find(DB::table('orders')->max('id'));

(Note that if the id field is not unique, you will only get one row back - this is because find() will only return the first result from the SQL server.).

shareimprove this answeredited Dec 4 '16 at 22:32answered Apr 15 '14 at 2:08timgws4,78712847

   it is possible to make it chain toArray() ? I got an error if I append with ->toArray(). – Shiro Apr 15 '14 at 3:27   @Shiro toArray() is a model method of Eloquent, not of DB. You will need to use whereRaw() instead. I have updated my answer. – timgws Apr 15 '14 at 5:05    thanks for your answer, may I know why u put array(25) for it? – Shiro Apr 15 '14 at 5:492 ah, typo from c&p'd source code. – timgws Apr 15 '14 at 6:22   Raw queries are not necessary here, one of the other answers are a better solution. – Erik Berkun-DrevnigDec 2 '16 at 20:50

Just like the docs say


shareimprove this answeranswered Apr 15 '14 at 1:40Ohgodwhy28.8k52755

   I would like to get the whole record,not only the max id, and I do not want make it two queries. – Shiro Apr 15 '14 at 1:471 What is wrong with this query? – timgws Apr 15 '14 at 2:07   @Shiro Sorry, you will have to perform a query chain for this, as laravel's fluent query builder will not allow this to be done as a single string, good luck – Ohgodwhy Apr 15 '14 at 2:17   Thanks for being simple ! – ihue Aug 3 '16 at 15:53

No need to use sub query, just Try this,Its working fine:

  DB::table('orders')->orderBy('id', 'desc')->first();
shareimprove this answeranswered Jun 16 '16 at 11:16Govind Samrow3,59962042

Orders::max('id');

I used it is short and best;

shareimprove this answeranswered Apr 22 at 18:26Afraz10011

For objects you can nest the queries:

DB::table('orders')->find(DB::table('orders')->max('id'));

So the inside query looks up the max id in the table and then passes that to the find, which gets you back the object.

shareimprove this answeranswered Feb 28 '16 at 10:24tristanbailey2,65311726

来自  https://stackoverflow.com/questions/23073214/laravel-query-builder-where-max-id


普通分类: