
Update if exist else insert new record in laravel
-
UpdateOrCreate method in laravel
--PATH app\Http\Controllers\<YourController>.php$User = User::updateOrCreate( ['username' => $request->username], ['email' => $request->email, 'password' => bcrypt($request->password)] );
0Laravel 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.
Random Code Snippet Queries: Laravel
- Return view from route Laravel
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Laravel 9 pagination with search filter
- Json encode method in laravel
- How to fetch single row data from database in laravel
- Update existing pivot table data in laravel
- Route group with URI prefix using middleware and route name prefixes
- How to customize pagination view in laravel
- Get previous date data in laravel
- How to get specific columns using with method in laravel Eloquent relationship
- Ajax POST request in laravel
- Get content from web URL in laravel
- How to prevent host header attack in Laravel
- How to pass data to partial view file in laravel
- How to insert dynamic values to additional column with pivot column in pivot table on multiple records
- Get count of filter data, while return a small set of records
- How to upload image in laravel 8
- How to get images from AWS s3 and display in Laravel blade
- How to check if user has created any post or not in laravel
- How to change default timestamp fields name in Laravel
- Attempt to read property "avatar" on null in Laravel
- How to create static page in Laravel
- Delete file from amazon s3 bucket using Laravel
- Shorter syntax for whereHas with call back function in laravel
- PhpMyAdmin - Error The mysqli extension is missing