{!! Auth::user()->myClass()->get()->lists('custom_name','id')->toJson() !!}
返回类似
的内容{"1":"Category G - George Costanza","2":"Category "ABc" - Dwayne Johnson"}
尝试使用javascripts JSON.parse
SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 ...
不应该toJson()
返回转义报价为:
{"1":"Category G - George Costanza","2":"Category \"ABc\" - Dwayne Johnson"}
我希望我在这里遗漏一些基本的东西。
编辑:
自定义访问者:
public function getCustomNameAttribute(){
return $this->parentModel->name . ' - ' . $this->name;
}
myClass是manyToMany关系,否则可以正常工作。据我所知。
答案 0 :(得分:0)
代替此{!! Auth::user()->myClass()->get()->lists('custom_name','id')->toJson() !!}
使用{!! !!}
或{{ }}
使用<?= ?>
它们将产生的json用额外的双引号""
而不是单引号''
括起来,因此parseJSON
将无法解析它。
因此您可以使用它
var javascript_var = $.parseJSON('<?= Auth::user()->myClass()->get()->pluck('custom_name','id')->toJson() ?>');
正在使用php <?= ?>
的速记回显功能