简体   繁体   English

Laravel中的hasManyThrough关系导致更新失败

[英]Update failed with hasManyThrough relationship in laravel

I'm trying to update child model with Laravel hasManyThough. 我正在尝试使用Laravel hasManyThough更新子模型。

What I've tried 我尝试过的

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
    ]);

What I'm getting 我得到什么

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. 我使用的是laravel 5.6,模型是artisan make:model提供的默认artisan make:model

Update: 更新:

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. 字段列表中的“ customer_id”列不明确,即两个表中都存在用于内部联接的customer_id字段。 Use customer_id with table name using Dot (.) operator. 通过点(。)运算符将customer_id与表名一起使用。

update invoice_items inner join invoices on invoices . 更新invoice_items内部联接invoicesinvoices id = invoice_items . id = invoice_items invoice_id set invoices. invoice_id设置发票。 customer_id = 2, invoices. customer_id = 2,发票。 user_id = 2, updated_at = 2018-09-21 22:03:45 where invoices . user_id = 2, updated_at = 2018-09-21 22:03:45 invoices order_id = 1) order_id = 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM