Laravel Checking If a Record Exists

后端 未结 26 2069
礼貌的吻别
礼貌的吻别 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:07

    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.

    0 讨论(0)
  • 2020-11-28 18:07
    $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.

    0 讨论(0)
  • 2020-11-28 18:09

    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';
        }
    
    
    0 讨论(0)
  • 2020-11-28 18:10

    Laravel 6 or on the top: Write the table name, then give where clause condition for instance where('id', $request->id)

     public function store(Request $request)
        {
    
            $target = DB:: table('categories')
                    ->where('title', $request->name)
                    ->get()->first();
            if ($target === null) { // do what ever you need to do
                $cat = new Category();
                $cat->title = $request->input('name');
                $cat->parent_id = $request->input('parent_id');
                $cat->user_id=auth()->user()->id;
                $cat->save();
                return redirect(route('cats.app'))->with('success', 'App created successfully.');
    
            }else{ // match found 
                return redirect(route('cats.app'))->with('error', 'App already exists.');
            }
    
        }
    
    0 讨论(0)
  • 2020-11-28 18:10

    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();
    
    0 讨论(0)
  • 2020-11-28 18:12

    Simple, comfortable and understandable with Validator

    class CustomerController extends Controller
    {
        public function register(Request $request)
        {
    
            $validator = Validator::make($request->all(), [
                'name' => 'required|string|max:255',
                'email' => 'required|string|email|max:255|unique:customers',
                'phone' => 'required|string|max:255|unique:customers',
                'password' => 'required|string|min:6|confirmed',
            ]);
    
            if ($validator->fails()) {
                return response(['errors' => $validator->errors()->all()], 422);
            }
    
    0 讨论(0)
提交回复
热议问题