我刚来拉拉维尔。如何查找是否存在记录?

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

我能做什么来查看$user是否有记录?


当前回答

$user = User::where('email', request('email'))->first();
return (count($user) > 0 ? 'Email Exist' : 'Email Not Exist');

其他回答

$userCnt     = User::where("id",1)->count();
if( $userCnt ==0 ){
     //////////record not exists 
}else{
      //////////record exists 
}

注:其中条件根据您的要求。

我解决了这个问题,使用empty()函数:

$user = User::where('email', Input::get('email'))->get()->first();
//for example:
if (!empty($user))
    User::destroy($user->id);
if ($u = User::where('email', '=', $value)->first())
{
   // do something with $u
   return 'exists';
} else {
  return 'nope';
}

可以用try/catch吗

->get()仍然返回一个空数组

$user = User::where('email', request('email'))->first();
return (count($user) > 0 ? 'Email Exist' : 'Email Not Exist');

这是一个简单的代码来检查电子邮件是否存在数据库


    $data = $request->all();
    $user = DB::table('User')->pluck('email')->toArray();
    if(in_array($user,$data['email']))
    {
    echo 'existed email';
    }