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

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

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


当前回答

最好的解决方案之一是使用firstOrNew或firstOrCreate方法。文档中有更多关于这两者的详细信息。

其他回答

if($user->isEmpty()){
    // has no records
}

Eloquent使用集合。 参见以下链接:https://laravel.com/docs/5.4/eloquent-collections

最好的解决方案之一是使用firstOrNew或firstOrCreate方法。文档中有更多关于这两者的详细信息。

if (User::where('email', 'user@email.com')->first()) {
    // It exists
} else {
    // It does not exist
}

如果只需要检查是否存在,请使用first(),而不要使用count()。

First()更快,因为它检查单个匹配,而count()计数所有匹配。

这将检查用户表中是否存在所请求的电子邮件:

if (User::where('email', $request->email)->exists()) {
   //email exists in user table
}

拉拉维尔 5.6.26v

通过主键(电子邮件或id)查找现有记录

    $user = DB::table('users')->where('email',$email)->first();

然后

      if(!$user){
             //user is not found 
      }
      if($user){
             // user found 
      }

包括“use DB”和表名user变成复数,使用上面的查询,就像user变成users