如何在laravel中获取json值
JSON:{“faktor”:[“sds”],“presentase”:[“23”]}如何在laravel中获取json值,laravel,laravel-5,Laravel,Laravel 5,JSON:{“faktor”:[“sds”],“presentase”:[“23”]} Laravel控制器: public static function factor_task($id_project, $urutan) { return (DB::table('project_faktor') ->select('faktor_presentase') -
Laravel控制器:
public static function factor_task($id_project, $urutan)
{
return (DB::table('project_faktor') ->select('faktor_presentase')
->where('id_project', '=', $id_project)
->where('urutan', '=', $urutan)
->get());
}
$id_project = $projects->id_project;
$array_delay_factors = (json_decode(App\Http\Controllers\ProjectController::factor_task('51', '1'),true));
$array_delay_factors_count = $array_delay_factors[0]['faktor_presentase'];
echo ($array_delay_factors_count->faktor);
刀片视图:
public static function factor_task($id_project, $urutan)
{
return (DB::table('project_faktor') ->select('faktor_presentase')
->where('id_project', '=', $id_project)
->where('urutan', '=', $urutan)
->get());
}
$id_project = $projects->id_project;
$array_delay_factors = (json_decode(App\Http\Controllers\ProjectController::factor_task('51', '1'),true));
$array_delay_factors_count = $array_delay_factors[0]['faktor_presentase'];
echo ($array_delay_factors_count->faktor);
但是,它显示了这样的错误
正在尝试获取非对象(0)的属性“faktor”
您拥有从该方法返回的
stdClass
对象的集合。如果只需要第一个对象和单个值:
App\Http\Controllers\ProjectController::factor_task('51', '1')
->first()
->faktor_presentase;
faktor\u presentase
是您在该控制器方法的该查询中选择的唯一字段。这还假设至少返回一个结果。欢迎访问SO。。。为什么从factor\u task
返回时调用json\u decode
?当不使用json\u decode