如何从请求对象Laravel获取属性?

时间:2019-06-02 13:32:02

标签: laravel laravel-5

我有POST数据:

[{"id":1,"amount":0,"debit":10,"credit":0,"invoice":{"AC_code":"10","AC_name":"Qeyri-maddi aktivlər","AC_id":2}},
{"id":2,"amount":0,"debit":0,"credit":10,"invoice":{"AC_code":"18","AC_name":"Sair uzunmüddətli maliyyə aktivləri","AC_id":33}}]

我尝试这样迭代传入的数组:

public function update(Request $request, $id)
{
   foreach($request as $k => $value) {
     echo $value['amount']; // $value->amount
   }

}

但是我收到错误消息:

{"errors":"Cannot use object of type Symfony\\Component\\HttpFoundation\\ParameterBag as array"}

1 个答案:

答案 0 :(得分:2)

也许尝试一下:

public function update(Request $request, $id) {
    foreach($request->all() as $key => $value) {
        echo $value->amount;
    }
}

参考:https://laravel.com/docs/5.8/requests#retrieving-input