php 如何使用 Laravel 4 原始查询获取第一条记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19794098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:02:31  来源:igfitidea点击:

How to get the first record with laravel 4 raw queries

phplaravellaravel-4

提问by Iyad Al aqel

How can i get the first record with laravel 4 raw queries.

如何使用 Laravel 4 原始查询获取第一条记录。

Something like DB::select('')->first();is not working neither did DB::first('')

类似的东西DB::select('')->first();也不起作用DB::first('')

回答by Antonio Carlos Ribeiro

The fact is that DB:select() returns an array, so you have to:

事实是 DB:select() 返回一个数组,因此您必须:

DB::select(DB::raw('select * from users'))[0];

You also can

你也可以

DB::table('users')->first();

回答by nikoskip

Like this:

像这样:

DB::table('users')->take(1)->get()[0];

回答by undefined

DB::table('users')->first(0);

回答by Midel Group

Use like this

像这样使用

$allmarketers = ProductStock::take(3)->get()[2]; 

Take will return how many rows you want and you use the offset like 2 to pick individual rows

Take 将返回您想要的行数,您使用 2 之类的偏移量来选择单个行

回答by Cengkuru Michael

When using eloquent you can do this:

使用 eloquent 时,您可以这样做:

 $user = User::take(1)->get();