How to convert Laravel collection array to json

If you are looking into doing if for a blade template you can use the @json Blade directive, like so:

<script type="text/javascript">
    var PARAMS = @json($params)
</script>

P.S.Tested in Laravel 5.6


Couple of things you can do, you can use default method ->toJson() with the user collection like this

$users = DB::table('users')->get();
$users->toJson();

If you do have troubles with it them you can php build in json_encode method here is the full documentation http://php.net/manual/en/function.json-encode.php

$users = DB::table('users')->get();
json_encode($users)

Hope this helps!


Have a look at the documentation.

You can use toJson(),to convert the collection to json object.

$users = DB::table('users')->get()->toJson();
dd($users);

You can also can do this in simple php way by using json_encode function

$users = json_encode($users);

Have a look at this link

Greetings and happy coding!


add "use Response;"

return Response::json([
    'data' => $value
], 200);

hope this helps!