mercredi 9 mars 2016

Laravel - toJson() method not escaping quotes (") so I can't parse it with Javascript

{!! Auth::user()->myClass()->get()->lists('custom_name','id')->toJson() !!}

returns something like

{"1":"Category G - George Costanza","2":"Category "ABc" - Dwayne Johnson"}

this rises to an error when trying to parse it with javascripts JSON.parse

SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 ...

Shouldn't the toJson() return escaped quotes as:

{"1":"Category G - George Costanza","2":"Category \"ABc\" - Dwayne Johnson"}

I hope I'm missing something basic here.

EDIT:

custom Accessor:

public function getCustomNameAttribute(){
        return $this->parentModel->name . ' -  ' . $this->name;
    }

The myClass is manyToMany relation that otherwise works fine. As far as I can tell.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire