我是Laravel的新手.请原谅新手问题,但如何查找记录是否存在?
$user = User::where('email', '=', Input::get('email'));
Run Code Online (Sandbox Code Playgroud)
我该怎么办才能看到是否$user有记录?
我想得到条件匹配的表中的第一行:
User::where('mobile', Input::get('mobile'))->first()
Run Code Online (Sandbox Code Playgroud)
它运行良好,但如果条件不匹配,则会抛出异常:
ErrorException
Trying to get property of non-object
Run Code Online (Sandbox Code Playgroud)
目前我这样解决:
if (User::where('mobile', Input::get('mobile'))->exists()) {
$user = User::where('mobile', Input::get('mobile'))->first()
}
Run Code Online (Sandbox Code Playgroud)
我可以不运行两个查询吗?