Php 雄辩->;第一个()如果->;存在()
我想获取表中条件匹配的第一行:Php 雄辩->;第一个()如果->;存在(),php,laravel,eloquent,Php,Laravel,Eloquent,我想获取表中条件匹配的第一行: User::where('mobile', Input::get('mobile'))->first() 它工作正常,但如果条件不匹配,则会引发异常: ErrorException Trying to get property of non-object 目前我是这样解决的: if (User::where('mobile', Input::get('mobile'))->exists()) { $user = User::where('m
User::where('mobile', Input::get('mobile'))->first()
它工作正常,但如果条件不匹配,则会引发异常:
ErrorException
Trying to get property of non-object
目前我是这样解决的:
if (User::where('mobile', Input::get('mobile'))->exists()) {
$user = User::where('mobile', Input::get('mobile'))->first()
}
我可以在不运行两个查询的情况下执行此操作吗?注意:first()方法不会引发原始问题中描述的异常。如果您遇到此类异常,则代码中还有另一个错误。
选择user first()并检查结果的正确方法:
$user = User::where('mobile', Input::get('mobile'))->first(); // model or null
if (!$user) {
// Do stuff if it doesn't exist.
}
其他技术(不推荐,不必要的开销):
或
或
每一种方法都是获得所需结果的不同方式。(ps-我无法评论)我认为你最好的选择与你所做的类似,或者类似于:
$user = User::where('mobile', Input::get('mobile'));
$user->exists() and $user = $user->first();
哦,还有:
count()
如果存在,则取而代之,但这可能是在get
get
返回Collection
后使用的,而应该是获取多行
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
答案已经被接受,但在这些情况下,我认为更优雅的解决方案是使用错误处理
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());
}
用一种简单的方式尝试一下,它会起作用的
$userset = User::where('name',$data['name'])->first();
if(!$userset) echo "no user found";
如我所说,如果没有找到结果,第一行抛出异常。@webin此版本应始终返回一个雄辩的\Collection,允许您始终对其调用方法isEmpty()。是的,谢谢,(…然后我应使用$user->first()
)获取第一行,因此first()
不返回集合?@dangel no first()将不返回集合这将返回初始集合中的第一个模型对象(如果有)。感谢您的评论:)请参阅Matt的答案,这是一种方法,在我的代码中,对db的查询是双倍的且效率低下。嗯,是的,当然这就是存在异常的原因!但我需要知道如何用有说服力的方法来具体解决这个问题。感谢first()
没有引发异常,如果没有找到任何内容,它将返回null
,因此这将不起作用。正确使用的方法是firstOrFail()
,该方法在接受的答案中引用。在问题中,如果条件不匹配,则会引发ErrorException,因此我假设某些内容被覆盖。原始问题的假设不正确(或者他们使用的是Laravel 4之前的Laravel的早期版本)。您的第一个示例不正确,因为first()返回一个对象或null。您不应该对其调用count()。对于第三个示例,您可以在这种情况下使用count(),但最好使用$users->count()@orrd是的,它返回model或null。为什么这是最简单的方法?看,显然是正确的。有人可能会说它是间接的,但情况不同。我不知道你使用的是什么版本的Laravel,但首先()如果表没有匹配的行,则不会引发异常,我知道至少从Laravel 4.2开始就没有。它只返回null。可能是由于代码中的其他问题导致了异常。我认为您可能需要包括您的用户模型,如果您的控制器上没有,请使用App\Models\User;这可能是原因排除例外
$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
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());
}
$userset = User::where('name',$data['name'])->first();
if(!$userset) echo "no user found";