php Eloquent ->first() 如果 ->exists()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24531312/
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
Eloquent ->first() if ->exists()
提问by Webinan
I want to get the first row in table where condition matches:
我想获取条件匹配的表中的第一行:
User::where('mobile', Input::get('mobile'))->first()
It works well, but if the condition doesn't match, it throws an Exception:
它运行良好,但如果条件不匹配,则会抛出异常:
ErrorException
Trying to get property of non-object
Currently I resolve it like this:
目前我是这样解决的:
if (User::where('mobile', Input::get('mobile'))->exists()) {
$user = User::where('mobile', Input::get('mobile'))->first()
}
Can I do this without running two queries?
我可以在不运行两个查询的情况下执行此操作吗?
回答by Matt Burrow
Note: The first() method doesn't throw an exception as described in the original question. If you're getting this kind of exception, there is another error in your code.
注意: first() 方法不会像原始问题中描述的那样抛出异常。如果您收到此类异常,则您的代码中存在另一个错误。
The correct way to user first() and check for a result:
使用 first() 并检查结果的正确方法:
$user = User::where('mobile', Input::get('mobile'))->first(); // model or null
if (!$user) {
// Do stuff if it doesn't exist.
}
Other techniques (not recommended, unnecessary overhead):
其他技术(不推荐,不必要的开销):
$user = User::where('mobile', Input::get('mobile'))->get();
if (!$user->isEmpty()){
$firstUser = $user->first()
}
or
或者
try {
$user = User::where('mobile', Input::get('mobile'))->firstOrFail();
// Do stuff when user exists.
} catch (ErrorException $e) {
// Do stuff if it doesn't exist.
}
or
或者
// Use either one of the below.
$users = User::where('mobile', Input::get('mobile'))->get(); //Collection
if (count($users)){
// Use the collection, to get the first item use $users->first().
// Use the model if you used ->first();
}
Each one is a different way to get your required result.
每一种都是获得所需结果的不同方式。
回答by ash
(ps - I couldn't comment) I think your best bet is something like you've done, or similar to:
(ps - 我无法发表评论)我认为你最好的选择是你做过的事情,或者类似于:
$user = User::where('mobile', Input::get('mobile'));
$user->exists() and $user = $user->first();
Oh, also: count()
instead if exists
but this could be something used after get
.
哦,还有:count()
如果exists
但这可以在get
.
回答by Jarek Tkaczyk
get
returns Collection
and is rather supposed to fetch multiple rows.
get
返回Collection
并且应该获取多行。
count
is a generic way of checking the result:
count
是检查结果的通用方法:
$user = User::where(...)->first(); // returns Model or null
if (count($user)) // do what you want with $user
// or use this:
$user = User::where(...)->firstOrFail(); // returns Model or throws ModelNotFoundException
// count will works with a collection of course:
$users = User::where(...)->get(); // returns Collection always (might be empty)
if (count($users)) // do what you want with $users
回答by Rashi Goyal
Try it this way in a simple manner it will work
以一种简单的方式尝试这种方式它会起作用
$userset = User::where('name',$data['name'])->first();
if(!$userset) echo "no user found";
回答by user1669496
An answer has already been accepted, but in these situations, a more elegant solution in my opinion would be to use error handling.
一个答案已经被接受,但在这些情况下,我认为更优雅的解决方案是使用错误处理。
try {
$user = User::where('mobile', Input::get('mobile'))->first();
} catch (ErrorException $e) {
// Do stuff here that you need to do if it doesn't exist.
return View::make('some.view')->with('msg', $e->getMessage());
}