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

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

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


当前回答

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

其他回答

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


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

最短工作选项:

// if you need to do something with the user 
if ($user = User::whereEmail(Input::get('email'))->first()) {

    // ...

}

// otherwise
$userExists = User::whereEmail(Input::get('email'))->exists();

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

简单地用这个来判断真假 $ user =用户::(“电子邮件”、“=”,输入::('邮件'))- >存在(); 如果你想要$user with result,你可以使用这个, $ user =用户::(“电子邮件”、“=”,输入::('邮件'))- > ();

像这样检查结果,

if(count($user)>0){}

你也可以用这个, $ user =用户::(“电子邮件”、“=”,输入::('邮件')); 如果存在($ user - > ()) { $user = $user->get(); }

创建下面的方法(为自己),以检查给定的记录id是否存在于Db表中。

private function isModelRecordExist($model, $recordId)
{
    if (!$recordId) return false;

    $count = $model->where(['id' => $recordId])->count();

    return $count ? true : false;
}

// To Test
$recordId = 5;
$status = $this->isModelRecordExist( (new MyTestModel()), $recordId);

有帮助!