samedi 16 juillet 2016

Unable to access object properties from URL params in Laravel

I am trying to access object properties from a request from my Angular app. I am using 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 of params:

enter image description here

Laravel:

public function getAnswers(Request $request)
{
    $input = $request->all();

    $question_objs = $input['questions'];

    foreach ($question_objs as $question_answer_object) {
        return $question_answer_object;

enter image description here

Looks like so far so good!


But if I try to access a property within laravel, like question_id:

return $question_answer_object['question_id'];

I get error:

"Illegal string offset 'question_id'

Laravel already parses the JSON, and when I return it, I can see it's an object. Why can't I access the properties? I've also tried json_decode without luck.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire