Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel updateOrCreate method

I have the following code in my method which I am sending via ajax to the controller method :

    $newUser = \App\UserInfo::updateOrCreate([         'user_id'   => Auth::user()->id,         'about'     => $request->get('about'),         'sec_email' => $request->get('sec_email'),         'gender'    => $request->get("gender"),         'country'   => $request->get('country'),         'dob'       => $request->get('dob'),         'address'   => $request->get('address'),         'mobile'    => $request->get('cell_no')     ]); 

The dd($request->all()) gives me :

array:8 [   "_token" => "fHeEPfTvgMD3FpIBmmc6DmKXFaiuWKZEiOhg6twQ"   "about" => "Some about me."   "sec_email" => "[email protected]"   "country" => "Priority highest"   "gender" => "male"   "dob" => "12/12/1990"   "address" => "Some address"   "cell_no" => "234234234" ] 

which is perfect.

Jquery code :

$('#submit-editProfile-form').on('click', function() {     var profileEditForm = $("#edit-user-profile");     var formData = $('#edit-user-profile').serialize();     profileEditForm.on('submit', function(e){         e.preventDefault();         $.ajaxSetup({             headers: {                 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')             }         });         $.ajax({             url:'/freelance/edit-userProfile-info',             type:'POST',             data:formData,             error: function (data) {                console.log('Error');             }         });     }).submit(); }); 

Now the problem is that i have a record in my table, But the above code creates another one, And the second is that it creates multiply by two records on each button click (request).

like image 636
Gammer Avatar asked Mar 09 '17 12:03

Gammer


People also ask

How does Laravel updateOrCreate work?

The updateOrCreate method attempts to find a Model matching the constraints passed as the first parameter. If a matching Model is found, it will update the match with the attributes passed as the second parameter.

How do you use create or update in Laravel?

Generally, in Laravel, we use the create() method to create new data and the update() method to update our data into the database. But there is a way to use both methods at a time like when it needs to create new data it uses the create() method and to update the data it uses the update().

How do I use firstOrNew in Laravel?

Laravel eloquent added amazing method call firstOrNew(). firstOrNew method help you to find record in database table and returns, if there is no records in database table then it will create new object instance and using save() you can store in database and return.

How do you update a record in Laravel?

We can update the records using the DB facade with update method. The syntax of update method is as shown in the following table. Run an update statement against the database.


1 Answers

In your use case, you should specify a second parameter. The first indicates the conditions for a match and second is used to specify which fields to update.

$newUser = \App\UserInfo::updateOrCreate([     //Add unique field combo to match here     //For example, perhaps you only want one entry per user:     'user_id'   => Auth::user()->id, ],[     'about'     => $request->get('about'),     'sec_email' => $request->get('sec_email'),     'gender'    => $request->get("gender"),     'country'   => $request->get('country'),     'dob'       => $request->get('dob'),     'address'   => $request->get('address'),     'mobile'    => $request->get('cell_no') ]); 

Here is an example from the documentation: https://laravel.com/docs/5.4/eloquent

// If there's a flight from Oakland to San Diego, set the price to $99. // If no matching model exists, create one. $flight = App\Flight::updateOrCreate(     ['departure' => 'Oakland', 'destination' => 'San Diego'],     ['price' => 99] ); 
like image 154
Rhu Avatar answered Oct 01 '22 06:10

Rhu