请帮我清理这个怪物!我试图让我的用户数组从 Laravel 集合到 JavaScript 变量。
这是我得到这个工作的唯一方法:
<script>
var obj = {
users: [<?php for($i = 0; $i < count($users); $i++) { echo $users[$i]->getObject(); if ($i + 1 < count($users)) { echo ','; } } ?>],
}
</script>
这给了我正确的输出:
users: [{"id":1,"first_name":"A","last_name":"A","birthday":"1970-02-12","gender":"M","username":"aa","email":"a@a.com","permissions":null,"activated":true,"activated_at":null},{"id":2,"first_name":"b","last_name":"b","birthday":"1982-01-10","gender":"M","username":"bb","email":"b@b.com"}]
我正在使用Robclancy/Presenter这意味着发送到视图时的对象最终成为演示者,因此我需要将对象取回$user->getObject();
从我的控制器$users = User::all();
返回用户对象的集合。
我试着做
<script>
var obj = {
users: <?php echo $users; ?>,
}
</script>
但这只是给了我
var obj = [{},{}];