【问题标题】:Laravel multiple records update with EloquentLaravel 使用 Eloquent 更新多条记录
【发布时间】:2016-09-13 08:04:26
【问题描述】:

我想Update多条记录到引用表。这是$request的回复。

array:21 [
  "_method" => "PATCH"
  "_token" => "xXukmVEsMvyeBODIURqFx9Mhk4LviYrV6iLmAuOY"
  "account_type" => "0"
  "name" => "test"
  "model_id" => "2"
  "location" => "USA"
  "serial_no" => "00055555"
  "status" => "Sales"
  "capacity_lower" => ""
  "weight" => "1212.000"
  "category" => "1"
  "attribute" => array:3 [
    1 => array:1 [
      1 => "1"
    ]
    3 => array:2 [
      3 => "5"
      2 => "2"
    ]
    4 => array:5 [
      5 => "11"
      6 => "13"
      4 => "9"
      7 => "23"
      8 => "37"
    ]
  ]
  "crane_manufacture_id" => "1"
  "condition" => "Average"
  "manufacture_year" => "2020"
  "unit_no" => "222222"
  "hours" => "100"
  "video_url" => ""
  "sub_category" => "4"
  "description" => "dsfs"
  "productImages" => array:7 [
    0 => array:3 [
      "id" => "27"
      "descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
      "status_id" => "2"
    ]
    1 => array:3 [
      "id" => "28"
      "descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
      "status_id" => "2"
    ]
    2 => array:3 [
      "id" => "29"
      "descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
      "status_id" => "2"
    ]
    3 => array:3 [
      "id" => "30"
      "descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
      "status_id" => "2"
    ]
    4 => array:3 [
      "id" => "31"
      "descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
      "status_id" => "2"
    ]
    5 => array:3 [
      "id" => "32"
      "descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
      "status_id" => "2"
    ]
    6 => array:3 [
      "id" => "33"
      "descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
      "status_id" => "2"
    ]
  ]
]

我有两个表,Products 和 product_images。我已经给出了一个产品有多个图像的关系。我在两个表中都应用了hasMany 关系。

现在在更新记录时,我按照以下代码更新 product_images 记录。

DB::enableQueryLog();
        $product = Product::find($id);
        //$product->save($request->input());
        $product->productImages()->update(new ProductImage($request->input('productImages')));
        dd(DB::getQueryLog());

但我在 Model.php 第 452 行中遇到“MassAssignmentException 错误: 0"

【问题讨论】:

    标签: laravel laravel-5 laravel-5.2


    【解决方案1】:

    检查您的模型 products 和 product_images 是否具有可填充属性。参考docs

    protected $fillable = ['list of columns to be inserted'];
    

    【讨论】:

      【解决方案2】:

      检查可填充属性protected $fillable = []; 或用于$request protected $guarded = []; 中的所有属性

      【讨论】:

        猜你喜欢
        • 2019-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-04
        • 2020-11-13
        • 1970-01-01
        • 2017-03-12
        • 2015-12-07
        相关资源
        最近更新 更多