Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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
Laravel-按自定义列查找或失败_Laravel_Laravel 4_Eloquent_Laravel 5 - Fatal编程技术网

Laravel-按自定义列查找或失败

Laravel-按自定义列查找或失败,laravel,laravel-4,eloquent,laravel-5,Laravel,Laravel 4,Eloquent,Laravel 5,有一个findOrFail()方法,如果找不到任何内容,将抛出404,例如: User::findOrFail(1); 如何通过自定义列或fail查找实体,如: Page::findBySlugOrFail('about'); 像这样尝试: Page::where('slug', '=', 'about')->firstOrFail(); 更新: 我目前正在使用Laravel6.9.0,我确认@jeff puckett是正确的。Where条款行得通。这就是我的修补匠的工作原理 >

有一个
findOrFail()
方法,如果找不到任何内容,将抛出404,例如:

User::findOrFail(1);
如何通过自定义列或fail查找实体,如:

Page::findBySlugOrFail('about');
像这样尝试:

Page::where('slug', '=', 'about')->firstOrFail();
更新: 我目前正在使用Laravel6.9.0,我确认@jeff puckett是正确的。Where条款行得通。这就是我的修补匠的工作原理

>>> \App\Models\User::findOrFail('123b5545-5adc-4c59-9a27-00d035c1d212');
>>> App\Models\User
     id: "123b5545-5adc-4c59-9a27-00d035c1d212",
     name: "John",
     surname: "Graham",
     email: "john.graham@test.com",
     email_verified_at: "2020-01-03 16:01:53",
     created_at: "2020-01-03 16:01:59",
     updated_at: "2020-01-03 16:01:59",
     deleted_at: null,

>>> \App\Models\User::where('name', 'Buraco')->findOrFail('123b5545-5adc-4c59-9a27-00d035c1d212');
>>> Illuminate/Database/Eloquent/ModelNotFoundException with message 'No query results for model [App/Models/User] 123b5545-5adc-4c59-9a27-00d035c1d212'


>>> \App\Models\User::where('name', 'John')->findOrFail('123b5545-5adc-4c59-9a27-00d035c1d212');
>>> App\Models\User
     id: "123b5545-5adc-4c59-9a27-00d035c1d212",
     name: "John",
     surname: "Graham",
     email: "john.graham@test.com",
     email_verified_at: "2020-01-03 16:01:53",
     created_at: "2020-01-03 16:01:59",
     updated_at: "2020-01-03 16:01:59",
     deleted_at: null,
过时:
至少花了两个小时才意识到,如果在Laravel 5.6中的where()之后链接firstOrFail()方法,它基本上会尝试检索表的第一条记录并删除where子句。所以在何处之前先打电话或失败

Model::firstOrFail()->where('something', $value)

Page::where('about','data')->firstOrFail()这应该适用于相等运算符“=”无需放置它explicitly@Alupotha您还可以执行
Page::wherebout('data')->firstOrFail()我们需要更多在模型之后工作的where方法的示例。有没有关于拉威尔的文章。。。有知识库吗?嗯,我想你一定还有别的问题。在laravel v5.6.23中,where子句going first对我来说很好!这是可行的,但区别在于,在本例中,查询数据库,然后使用集合的
where
helper。我的意思是在数据库变成集合之前用
where
子句查询数据库。