Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从特定where子句获取最高id_Php_Laravel_Laravel 5_Laravel 5.1 - Fatal编程技术网

Php 从特定where子句获取最高id

Php 从特定where子句获取最高id,php,laravel,laravel-5,laravel-5.1,Php,Laravel,Laravel 5,Laravel 5.1,我的来宾数据库中有4条记录 我正在尝试查询具有note\u display=1且id最高的来宾 我试过了 我得到 我现在是一个lil,任何提示都会有很大的帮助?原始sql字符串应该类似于SELECT*FROM guests WHERE note_display=1 ORDER BY id DESC LIMIT 1看起来您在子查询中只得到note_display=1的行,其中id={表中存在的最大id}它选择注释显示为=0的最大id,因此发生了“尝试获取非对象”错误 如果你坚持使用raw试试

我的来宾数据库中有4条记录

我正在尝试查询具有
note\u display=1
且id最高的来宾


我试过了 我得到



我现在是一个lil,任何提示都会有很大的帮助?

原始sql字符串应该类似于
SELECT*FROM guests WHERE note_display=1 ORDER BY id DESC LIMIT 1
看起来您在子查询
中只得到
note_display=1
的行,其中id={表中存在的最大id}

它选择注释显示为=0的最大id,因此发生了“尝试获取非对象”错误

如果你坚持使用
raw
试试这个

$last_note = DB::table('guests')->where('id', DB::raw("(select max(`id`) from guests where note_display = '1')"))->first();

在此查询中不需要使用
raw
。您可以运行一个简单的查询,如

Guest::where('note_display', 1)->orderBy('id', 'desc')->first();

它将返回具有最高
ID
且具有
note\u display
=
1

的来宾,也许我没有正确考虑它,但不会
Guest::where('note\u display',1)->orderBy('ID',desc')->first()给你你需要的吗?@camelCase这正是我在读问题时所想的。我不知道为什么OP会使用原始语句来处理查询生成器如此容易完成的事情。你们完全正确。我让这比实际情况更难。我完全忘记了
orderBy('id','desc')
很容易做到!我去过那里很多次,很高兴我们能帮上忙。另外一个原因是我仍然保持我的DB:raw,并且仍然能想出解决方案。:)@我很高兴你找到了解决办法,我的朋友
$last_note = DB::table('guests')->where('id', DB::raw("(select max(`id`) from guests where note_display = '1')"))->first();
Guest::where('note_display', 1)->orderBy('id', 'desc')->first();