PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 25, 2022

[FIXED] why my updateOrInsert doesn't work laravel

 January 25, 2022     eloquent, laravel, laravel-5     No comments   

Issue

I use updateOrInsert to avoid duplicate data, why doesn't the Update function work and always insert data?

foreach($datas as $data){
                    DB::table('users')->updateOrInsert([
                        'user_connect_id' => $user->connect_id,
                        'description' => $data['description'],
                        'created_by' => $login->name,
                        'modified_by' => $login->name,
                        'created_at' => Carbon::now(),
                    ]);
                }

Solution

Check this out updateOrInsert, you need to two parameters, one is the matching attributes, the other is your array.

updateOrInsert(array $attributes, array $values = []) 

example:

DB::table('users')->updateOrInsert(['user_connect_id' => $user->connect_id],
                     [
                        'user_connect_id' => $user->connect_id,
                        'description' => $data['description'],
                        'created_by' => $login->name,
                        'modified_by' => $login->name,
                        'created_at' => Carbon::now(),
                     ]);


Answered By - TsaiKoga
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing