Laravel Checking If a Record Exists

2019-01-08 04:18发布

问题:

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

<?php

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

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

回答1:

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
}


回答2:

One of the best solution is to use the firstOrNew or firstOrCreate method.



回答3:

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

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



回答4:

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


回答5:

Laravel 5.6.26v

to find the existing record through primary key ( email or 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



回答6:

It's simple to get to know if there are any records or not

$user = User::where('email', '=', Input::get('email'))->get();
if(count($user) > 0)
{
echo "There is data";
}
else
echo "No data";


回答7:

In your Controller

$this->validate($request, [
        'email' => 'required|unique:user|email',
    ]); 

In your View - Display Already Exist Message

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif


回答8:

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


回答9:

you can use laravel validation .

But this code is also good:

$user = User::where('email',  $request->input('email'))->count();

if($user > 0)
{
   echo "There is data";
}
else
   echo "No data";


回答10:

This will check if particular email address exist in the table:

if (isset(User::where('email', Input::get('email'))->value('email')))
{
    // Input::get('email') exist in the table 
}


回答11:

Checking for null within if statement prevents Laravel from returning 404 immediately after the query is over.

if ( User::find( $userId ) === null ) {

    return "user does not exist";
}
else {
    $user = User::find( $userId );

    return $user;
}

It seems like it runs double query if the user is found, but I can't seem to find any other reliable solution.



回答12:

$user = User::where('email', '=', Input::get('email'))->first();
if ($user === null) {
   // user doesn't exist
}

can be written as

if (User::where('email', '=', Input::get('email'))->first() === null) {
   // user doesn't exist
}

This will return true or false without assigning a temporary variable if that is all you are using $user for in the original statement.



回答13:

Shortest working options:

// 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();


回答14:

this is simple code to check email is exist or not in database


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