I'm trying to update child model with Laravel hasManyThough.
The Base model has the relationship like below:
public function transactions()
{
return $this->hasManyThrough('App\Transaction', 'App\Invoice');
}
and I use below code to update child items.
$order->transactions()->update([
'customer_id' => 100,
'user_id' => 100
]);
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'customer_id' in field list is ambiguous (SQL: update `invoice_items` inner join `invoices` on `invoices`.`id` = `invoice_items`.`invoice_id` set `customer_id` = 2, `user_id` = 2, `updated_at` = 2018-09-21 22:03:45 where `invoices`.`order_id` = 1)
I'm using laravel 5.6 and the model is default as artisan make:model
provide.
Table structure
orders
- id
- user_id
- customer_id
- content
invoices
- id
- order_id
- user_id
- customer_id
- content
transactions
- id
- invoice_id
- user_id
- customer_id
- content
For now, I made a temp solution for this issue.
foreach ($order->transactions as $transaction) {
$transaction->update($updateRules);
}
Please feel free to post your solution.
Here is solution:
Column 'customer_id' in field list is ambiguous,ie customer_id field exists in both the tables using for inner join. Use customer_id with table name using Dot (.) operator.
update invoice_items
inner join invoices
on invoices
. id
= invoice_items
. invoice_id
set invoices. customer_id
= 2, invoices. user_id
= 2, updated_at
= 2018-09-21 22:03:45 where invoices
. order_id
= 1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.