Laravel使用Eloquent更新多个记录

时间:2016-09-13 08:04:26

标签: laravel laravel-5 laravel-5.2

我希望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

2 个答案:

答案 0 :(得分:1)

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

margin-top:-2px

答案 1 :(得分:0)

检查可填充属性protected $fillable = [];或使用$request protected $guarded = [];

中的所有属性
相关问题