Laravel Checking If a Record Exists

后端 未结 26 2272
礼貌的吻别
礼貌的吻别 2020-11-28 17:50

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

$user = User::where(\'em         


        
26条回答
  •  [愿得一人]
    2020-11-28 18:14

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

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

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

提交回复
热议问题