Laravel save() is not update record - laravel

I am trying to update records in the database table if record already exists. Insert new record if record is not to the database table.
I have written below code for that
DB::enableQueryLog();
$user->userCommission()->save(new UserCommission($input['commission']));
dd(DB::getQueryLog());
when EnableQueryLog it's always show insert query, If record is already in the table.
Here is my query..
array:1 [▼
0 => array:3 [▼
"query" => "insert into `user_commissions` (`created_by`, `status_id`, `percent`, `user_id`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?)"
"bindings" => array:6 [▼
0 => "1"
1 => "1"
2 => "0.10"
3 => 21
4 => "2016-08-13 08:07:45"
5 => "2016-08-13 08:07:45"
]
"time" => 2.57
]
]
In above query user_id 21 record already in the table although Save() insert record to the database.
where am i wrong with this code?
May I have to apply unique key to the table?

Require at least one unique column to find or update table record
$userCommission = UserCommission::firstOrNew(array('created_by' => 1));
$userCommission->status_id = 1;
$user()->userCommission()->save($userCommission);

There is a alternative one way step to check this.update_at & created_at will be inserted by laravel default
userCommission::updateOrCreate([
'user_id' => 21
],[
'created_by' => 1,
'status_id' => 1,
'percent' => "0.10",
'user_id' => 21,
]);

Related

Laravel query builder - bulk insert - Ignore columns not found

I am using laravel query builder where I am bulk inserting hundreds of rows. I want to insert the column found and ignore the columns not found in table
Here is my code:
$proposalOpsEvents = DB::table('table1')->where('proposal_id', $proposal->id)->get();
$proposalOpsEvents = $proposalOpsEvents->toArray();
DB::connection('mysql_archive')->table('archive_table1')->insert($proposalOpsEvents);
I get error "Unknown column".
In table 1 new columns are getting added dynamically. I want to ignore the newly added columns when inserting in archive_table1.
For example,
DB::table('archive_table1')->insert([
'email' => 'abc#example.com', //email column found - insert
'phone' => 0, // phone column found - insert
'address' => 'A'// address column (newly added) not found - ignore
]);
Any solution for these?
Have you tried the insertOrIgnore method?
DB::table('archive_table1')->insertOrIgnore([
'email' => 'abc#example.com', //email column found - insert
'phone' => 0, // phone column found - insert
'address' => 'A'// address column (newly added) not found - ignore
]);
You can use the DB::squemaBuilder
$columns=DB::connection('your_connection')->getSchemaBuilder()->getColumnListing('table_name');
This will return an array with all columns names of the table and with array manipulation you can intersect your columns to insert with columns in table.
$newData=[
'email' => 'abc#example.com', //email column found - insert
'phone' => 0, // phone column found - insert
'address' => 'A'// address column (newly added) not found - ignore
];
$newDataClean=array_intersect_key($newData, array_flip($columns));

Want to Join Two table without Knowing other Table Name Laravel

$users = DB::table('table1')->where('id', '=', '1')->get();
$joinedtable = DB::table('table1')
->join('' . $users[0]->tablename . '', 'table1.id', '=', '' . $users[0]->tablename . '.id')
->get();
I want to join two table, 1st table name is given and 2nd is comming from table1. I want to short this query and want to done using one query. Is it possible
Current O/P
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 1
[country] => table2
[test1] => 23423
[test2] => 234234
[newdata1] => 1
[newdata2] => 1
[newdata3] => 1
)
[1] => stdClass Object
(
[id] => 2
[country] => table3
[test1] => 123
[test2] => 123
[newdata1] => 2
[newdata2] => 2
[newdata3] => 2
)
)
First solution Option
Use pluck() to get the table name and then join the tables.
$table2 = DB::table('table1')->where('id', '=', '1')->pluck('columnname');

Batch insert in Laravel 5.2

I am using a API's with lot's of calculation almost 100 database fields at the end with a big Foreach loop.
In every iteration i insert data in database. I want to insert data in once at the end (Batch Insert like in CodeIgniter).
Any body have idea how to insert all data at the end of iteration. instead of every iteration it insert row in database.
I want to insert data at the end of loop. Any help or idea appreciated.
Use insert() method for bulk insertion. First, build an array with this structure:
$data = [
['name' => 'John', 'age' => 25],
['name' => 'Maria', 'age' => 31],
['name' => 'Julia', 'age' => 55],
];
Then insert the data using Eloquent model:
Model::insert($data);
Or using query builder:
DB::table('table_name')->insert($data);

Bulk Insert in Laravel 5.3

$list = [];
foreach($RoleDetails["Data"]["Permissions"] as $Permission) {
$MyModel = new UserRolePermissionModel();
$MyModel->UserID = $User->UserID;
$MyModel->RolePermissionID = $Permission->RolePermissionID;
$MyModel->IsActive = $Permission->IsActive;
array_push($list, $MyModel);
}
\DB::table('tbluserrolepermission')->insert($list);
Below are the error details
QueryException {#293 ▼
#sql: "insert into `tbluserrolepermission` (`0`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `13`, `14`, `15`, `16`, `17`) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
#bindings: array:18 [▶]
#message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into `tbluserrolepermission` (`0`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `13`, `14`, `15`, `16`, `17`) values ({"UserID":21,"RolePermissionID":19,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":20,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":21,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":22,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":23,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":24,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":25,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":26,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":27,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":28,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":29,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":30,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":31,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":32,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":33,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":34,"IsActive":1,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":35,"IsActive":1,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":36,"IsActive":1,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}))"
#code: "42S22"
#file: "C:\xampp\htdocs\AS4\vendor\laravel\framework\src\Illuminate\Database\Connection.php"
#line: 761
-previous: PDOException {#356 ▶}
+errorInfo: array:3 [▶]
+"previous": PDOException {#356 ▶}
-trace: {▶}
}
It is not working, because you are not providing an array of arrays with only the values matching the column names to populate the database with.
Example
Lets say you want to populate an array of users and save them to the database. The following example would do that.
DB::table('users')->insert([
['email' => 'taylor#example.com', 'votes' => 0],
['email' => 'dayle#example.com', 'votes' => 0]
]);
Note that email and votes in the above example are 2 columns in the users table. Note also that only an array of other arrays are passed to the insert method. What you are trying to insert is actually an array of eloquent model objects.
Source: https://laravel.com/docs/5.3/queries#inserts
I fixed it like below.
foreach($RoleDetails["Data"]["RolePermissions"] as $RolePermission) {
$data = [
'UserID' => $User->UserID,
'RolePermissionID' => $RolePermission->RolePermissionID,
'IsActive' => $RolePermission->IsActive,
'IsProtectionAvailable' => $RolePermission->IsProtectionAvailable,
'IsProtectionReadOnly' => $RolePermission->IsProtectionReadOnly
];
array_push($list,$data);
}
\DB::table('tbluserrolepermission')->insert($list);
or it could be like this.
UserRolePermissionModel::insert($list);
Can you try this?
UserRolePermissionModel::insert($list->toArray());
OR
DB::table('table')->insert($list->toArray());

Batch_update primary key joining table

i'm having a problem with batch update function with codeigniter, i'm using a joining table for my products and categories, as I have a many to many relationship. I have searched high and low for an answer but still nothing so i have come here to ask more senior technicians for help.
I have 2 columns in a table "product_category" with "product_id" & "category_id" so I can link 1 product to many categories. I have managed to perform the insert query which inserts all the id's fine, but the update is not working. Here's my code:
model:
function update_product_cat($product, $cat_id) {
$data = array();
foreach( $product as $index => $value )
{
$data[] = array(
'product_id' => $value ,
'category_id' => $cat_id[ $index ]
);
}
$this->db->update_batch('product_category', $data,
'product_id');
}
array:
Array ( [0] => Array ( [product_id] => 327 [category_id] => 3 ) [1] => Array ( [product_id] => 327 [category_id] => 5 ) [2] => Array ( [product_id] => 327 [category_id] => 7 ))
My error code:
Error Number: 1062
Duplicate entry '327-3' for key 'PRIMARY'
UPDATE product_category SET category_id = CASE WHEN product_id = '327' THEN '3' WHEN product_id = '327' THEN '5' WHEN product_id = '327' THEN '7' ELSE category_id END WHERE product_id IN ('327','327','327')
Any help would be greatly appreciated:
thanks
I'm not 100% sure this is your entire problem, but it looks like the product_category table has product_id set as a primary key - where it looks like you are trying to enter many identical product_id's. If that were the case you should probably just be using a regular update not batch, not to mention removing/replacing the primary key.

Resources