Update if exist else insert new record in laravel
Update if exist else insert new record in laravel
We’ll attempt to use programming in this lesson to solve the "Update if exist else insert new record in laravel" puzzle.
-
UpdateOrCreate method in laravel
--PATH app\Http\Controllers\<YourController>.php$User = User::updateOrCreate( ['username' => $request->username], ['email' => $request->email, 'password' => bcrypt($request->password)] );
Laravel provides the method updateOrCreate to insert a new record if it does not exist and update if it exists. It has two parameters, conditions and fields and these parameters should be array. It tries to find a model matching the attributes you pass in the first parameter. If a model is not found, it automatically creates and saves a new record after applying attributes passed in the second parameter with the merging first argument array and updates the record if the record already exists in the table.
This code snippet creates a new user record if the user with this particular username is not exist in the table, else it update the user email and password columns value using the second argument array if the record exists in the table.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Target class [App\Http\Controllers\Auth\Request] does not exist.
- Send OTP using textlocal api in laravel
- How to check record exist or not in relationship table
- Laravel hasmany select not working
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given
- Save or update pivot table data with additional column in Laravel
- Attempt to read property "avatar" on null in Laravel
- Call to undefined relationship [user] on model [App\Models\Post]
- How to call Laravel route in jQuery
- How to create static page in Laravel
- Target class [admin] does not exist.
- Composer create project laravel/laravel example app
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- Laravel create table migration with model
- Illuminate\Database\QueryException could not find driver
- How to create laravel project using composer
- How to call controller function from view in Laravel
- How to check records exist in loaded relationship in Laravel blade view
- Seed database using SQL file in Laravel
- Trying to access array offset on value of type null error in laravel
- Order by multiple columns in Laravel
- Call to a member function pluck() on null
- Get Array of IDs from Eloquent Collection
- How to authenticate admin users in Laravel ?