Php 如何使用分形在Json中显示数组?
我想用数组将REST API数据保存到数据库,它的工作,所有值都保存到表中,但当我想用json查看结果时,仍然会出错 这是我的错误Php 如何使用分形在Json中显示数组?,php,json,laravel,transform,Php,Json,Laravel,Transform,我想用数组将REST API数据保存到数据库,它的工作,所有值都保存到表中,但当我想用json查看结果时,仍然会出错 这是我的错误 "message": "Argument 1 passed to App\\Transformers\\NewLoanOfferTransformer::transform() must be an instance of App\\Model\\AntiAttrition, array given, called in /home/insko23/testing
"message": "Argument 1 passed to App\\Transformers\\NewLoanOfferTransformer::transform() must be an instance of App\\Model\\AntiAttrition, array given, called in /home/insko23/testing.insko.my/vendor/league/fractal/src/Scope.php on line 338",
我的控制器
public function new\u loan\u offer(请求$Request,防损耗$anti损耗)
{
$new=数组映射(null,$request->mo\u id,$request->id\u clrt,$request->TaskID,$request->ACID,$request->CustIDNo);
foreach($new as$new){
$request=新的防磨损;
$request->mou id=$new[0];
$request->id_clrt=$new[1];
$request->TaskID=$new[2];
$request->ACID=$new[3];
$request->CustIDNo=$new[4];
$request->save();
}
$response=fractal()
->项目(新)
->transformWith(新的LoanOffertTransformer)
->toArray();
return response()->json($response,201);
}
在foreach循环中,避免对要迭代的数组和数组元素使用相同的名称,您可以将foreach($new as$new)
重命名为foreach($newArray as$new)
,或者对代码逻辑有意义的东西。也不要使用$new[0]
使用$new['mo_id']
引用数组的键那么$new
是一个数组,所以您可能不想将其传递给项()
在这种情况下,您想传递一个防磨损的实例