尝试在将数据数组传递给Laravel 5.2中的控制器时获取非对象的属性

时间:2016-05-10 10:34:17

标签: php eloquent laravel-5.2

当我将一个关联数据数组传递给控制器​​时,它显示错误 '试图获得非对象财产' 我的控制器功能是,

        public function storeWBS(Request $request) {
        foreach($request->all() as $value){
            $formValue                = new WorkBreakdownStructure;
            $formValue->form_field_id = $value->idea_id;
            $formValue->value         = $value->wbs_description;
            $formValue->save();

        return back();}

    }

我设置了变量' fillable'在模型中

  

' ID' ' form_field_id' '值'   如果我打印'$ formValue'与DD()我得到的结果

#parameters: array:4 [▼
  "_token" => "u6iwKspevWLiuI6CRPhd82c8xm0EYb6IGQQJX2aR"
  "idea_id" => array:3 [▼
    0 => "1"
    1 => "1"
    2 => "1"
  ]
  "wbs_description" => array:3 [▼
    0 => "Work Breakdown Structure #1 :"
    1 => "Work Breakdown Structure #2"
    2 => "Work Breakdown Structure #3"
  ]
  "percentage" => array:3 [▼
    0 => "50"
    1 => "12"
    2 => "12"
  ]
]

1 个答案:

答案 0 :(得分:1)

all()返回数组,而不是对象,因此您无法像现在一样访问它:

.... = $value->idea_id;

相反,您需要更改此设置并使用数组语法:

.... = $value['idea_id'];

修改

你也应该删除forach(),因为你基本上遍历数组是没有意义的:

public function storeWBS(Request $request) {
      $value = $request->all();
            $formValue                = new WorkBreakdownStructure;
            $formValue->form_field_id = $value->idea_id;
            $formValue->value         = $value->wbs_description;
            $formValue->save();

        return back();
}

您还需要修复return back();行。无论back()指的是什么,你最有可能错误地调用它。