无法从 Laravel 中的 URL 参数访问对象属性
Unable to access object properties from URL params in Laravel
我正在尝试通过我的 Angular 应用程序的请求访问对象属性。我正在使用 Laravel 5.1
Angular:
console.log('getQuestionAnswers', params);
return $http({
method: 'GET',
url: url + ver + '/questions/checkMany',
params: {
'questions[]' : params
},
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + $rootScope.access_token
},
cache: true
});
Console.log 个参数:
Laravel:
public function getAnswers(Request $request)
{
$input = $request->all();
$question_objs = $input['questions'];
foreach ($question_objs as $question_answer_object) {
return $question_answer_object;
对 Angular 的回应:return $question_objs;
对 Angular 的回应:return $question_answer_object;
到目前为止一切顺利!
但是如果我尝试访问 laravel 内的 属性,例如 question_id:
return $question_answer_object['question_id'];
我收到错误:
"Illegal string offset 'question_id'
Laravel 已经解析了 JSON、
// From Illuminate/Http/Request.php all() method:
if (! isset($this->json)) {
$this->json = new ParameterBag((array) json_decode($this->getContent(), true));
}
当我 return 它时,我可以看到它是一个对象。为什么我不能访问属性?我也试过 json_decode
没有运气。
用JSON解码:
$test = json_decode($question_answer_object, true);
return $test['question_id'];
这似乎可行。但是为什么?
像这样访问对象上的 属性:
return $question_answer_object->question_id;
出现以下错误:
"Trying to get property of non-object"
返回的问题是对象,不是数组。您必须使用 ->
访问它
return $question_answer_object->question_id;
$question_answer_object['question_id']
变量是一个包含JSON编码数据的字符串;要访问它,您需要先对其进行解码:
$decoded= json_decode($question_answer_object['question_id'], true);
return $decoded['question_id'];
如果您不以 application/json 的形式发送请求,请使用 $request->json()
。
您可以获得有关此问题的一些信息 here.
我正在尝试通过我的 Angular 应用程序的请求访问对象属性。我正在使用 Laravel 5.1
Angular:
console.log('getQuestionAnswers', params);
return $http({
method: 'GET',
url: url + ver + '/questions/checkMany',
params: {
'questions[]' : params
},
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + $rootScope.access_token
},
cache: true
});
Console.log 个参数:
Laravel:
public function getAnswers(Request $request)
{
$input = $request->all();
$question_objs = $input['questions'];
foreach ($question_objs as $question_answer_object) {
return $question_answer_object;
对 Angular 的回应:return $question_objs;
对 Angular 的回应:return $question_answer_object;
到目前为止一切顺利!
但是如果我尝试访问 laravel 内的 属性,例如 question_id:
return $question_answer_object['question_id'];
我收到错误:
"Illegal string offset 'question_id'
Laravel 已经解析了 JSON、
// From Illuminate/Http/Request.php all() method:
if (! isset($this->json)) {
$this->json = new ParameterBag((array) json_decode($this->getContent(), true));
}
当我 return 它时,我可以看到它是一个对象。为什么我不能访问属性?我也试过 json_decode
没有运气。
用JSON解码:
$test = json_decode($question_answer_object, true);
return $test['question_id'];
这似乎可行。但是为什么?
像这样访问对象上的 属性:
return $question_answer_object->question_id;
出现以下错误:
"Trying to get property of non-object"
返回的问题是对象,不是数组。您必须使用 ->
return $question_answer_object->question_id;
$question_answer_object['question_id']
变量是一个包含JSON编码数据的字符串;要访问它,您需要先对其进行解码:
$decoded= json_decode($question_answer_object['question_id'], true);
return $decoded['question_id'];
如果您不以 application/json 的形式发送请求,请使用 $request->json()
。
您可以获得有关此问题的一些信息 here.