php Laravel 检查记录是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27095090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:10:41  来源:igfitidea点击:

Laravel Checking If a Record Exists

phplaravellaravel-5eloquentconditional

提问by Ben

I am new to Laravel. Please excuse the newbie question but how do I find if a record exists?

我是 Laravel 的新手。请原谅新手问题,但我如何找到记录是否存在?

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

What can I do here to see if $userhas a record?

我可以在这里做什么以查看是否$user有记录?

回答by lukasgeiter

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
}

回答by tapos ghosh

One of the best solution is to use the firstOrNewor firstOrCreatemethod. The documentationhas more details on both.

最好的解决方案之一是使用firstOrNeworfirstOrCreate方法。该文档有关于两者的更多详细信息。

回答by lucidlogic

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

回答by Nabil

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

Eloquent uses collections. See the following link: https://laravel.com/docs/5.4/eloquent-collections

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

回答by doncadavona

if (User::where('email', '[email protected]')->first())
    // It exists
else
    // It does not exist

Use first(), not count()if you only need to check for existence.

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

first()is fasterbecause it checks for a single match whereas count()counts all matches.

first()更快,因为它会检查一个匹配而count()计数的所有比赛。

回答by Dhanushka Udayanga

In laravel eloquent, has default exists() method, refer followed example.

在 laravel eloquent 中,有默认的 exists() 方法,参考后面的例子。

if(User::where('id', $user_id )->exists()){ // your code... }

if(User::where('id', $user_id )->exists()){ // your code... }

回答by Nadeem Qasmi

Laravel 5.6.26v

Laravel 5.6.26v

to find the existing record through primary key ( email or id )

通过主键( email 或 id )查找现有记录

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

then

然后

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

include " use DB " and table name user become plural using the above query like user to users

包括“使用 DB”和表名 user 使用上述查询变成复数,如 user to users

回答by A. Zaman

This will check if requested email exist in the user table:

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

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

回答by Mohammad Usman

$email = User::find($request->email);
If($email->count()>0)
<h1>Email exist, please make new email address</h1>
endif

回答by Julian

if ($u = User::where('email', '=', $value)->first())
{
   // do something with $u
   return 'exists';
} else {
  return 'nope';
}

would work with try/catch

将与 try/catch 一起使用

->get() would still return an empty array

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