Laravel检查是否存在记录 [英] Laravel checking if record exists

查看:116
本文介绍了Laravel检查是否存在记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ user = User :: where('email','=',Input :: get('email')); 

我在这里可以查看 $ user 有记录?

解决方案

这取决于您是否要与用户协同工作,或仅检查是否存在。 / p>

如果要使用用户对象(如果存在):

  $ user = User :: where('email','=',Input :: get('email')) - > first(); 
if($ user === null){
//用户不存在
}

如果您只想检查

  if(User :: where('email' ,'=',Input :: get('email')) - > count()> 0){
//用户发现
}

甚至更好的

  if(User :: where('email','=',Input :: get('email')) - > exists()){
// user found
}


New to laravel, so excuse the newbie question but how do I find if a record exists?

$user = User::where('email', '=', Input::get('email'));

What would I do here to see if $user has a record?

解决方案

It depends if you want to work with the user afterwards or only check if one exists.

If you want to use the user object if it exists:

$user = User::where('email', '=', Input::get('email'))->first();
if ($user === null) {
   // user doesn't exist
}

And if you only want to check

if (User::where('email', '=', Input::get('email'))->count() > 0) {
   // user found
}

Or even nicer

if (User::where('email', '=', Input::get('email'))->exists()) {
   // user found
}

这篇关于Laravel检查是否存在记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆