Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Insert a new record if not exist and update if exist, laravel eloquent

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 11.1k
    Comment on it

    Laravel is very useful when we want to do basic operation .Suppose if we have a condition that we have to Insert a new record if not exist and update if exist, If we do this task in php then we have to put if-else condition But In Laravel 4.x we have done this by using one query.

    Syntax for inserting a new record if not exist and update if exist is:

    Syntax:

    $user = User::firstOrNew(ColumnName);
    

    Example:

      $user = User::firstOrNew(array('name' => Input::get('name')));
        $user->foo = Input::get('foo');
        $user->save();
    

    In above example User is the eloquent and save is the method which will save data in the database.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: