
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
- How to get CSRF token in laravel controller
- JQuery each loop on json response after ajax in laravel
- Display message with session flash using bootstrap alert class in laravel
- Laravel URL validation not working
- Call to a member function pluck() on null
- The openssl extension is required for SSL/TLS protection but is not available
- Laravel form request validation
- How to get session in blade Laravel ?
- Method Illuminate\Database\Eloquent\Collection::appends does not exist
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- How to get data from two tables in laravel
- How to add class to tr in table using foreach in laravel
- How to get the id of last record from collection object in laravel view
- Update email with unique validation in laravel
- How to get all route list
- How to pass data to multiple partial view files in laravel
- How to insert dynamic values to additional column with pivot column in pivot table on multiple records
- Method Illuminate\Database\Eloquent\Collection::lists does not exist
- Class App\Http\Controllers\Admin\UserController Does Not Exist
- Create project table with model and migration
- How to upload multiple images after preview in laravel using cropper js
- Get domain name in laravel
- Create project factory and seed data in laravel
- Send OTP using textlocal api in laravel
- Skip first n record and display rest records in laravel view