14

How can i convert this Laravel collection array to json format like below.

//All records from users table.
$users = DB::table('users')->get();

// Required json format.
return '{
          "data": [

            {
              "DT_RowId": "row_1",
              "id": "Tiger",
              "clear": "Nixon",
              "fsssf": "System Architect",
              "tex": "t.nixon@datatables.net",
              "created_at": "Edinburgh",
              "updated_at": "Edinburgh"
            },
             {
              "DT_RowId": "row_2",
              "id": "Tiger",
              "clear": "Nixon",
              "fsssf": "System Architect",
              "tex": "t.nixon@datatables.net",
              "created_at": "Edinburgh",
              "updated_at": "Edinburgh"
            }

          ],
  "options": [],
  "files": []
}';

Sorry for the basic question,but i am unable to convert this into this jso.

Khirad Zahra
  • 843
  • 2
  • 17
  • 42
  • $users is a collection and he has a method "->toJson()". – inet123 Sep 18 '17 at 07:49
  • In Laravel 9 you can still use the `@json` directive. Also you can find this in the Laravel docs: https://laravel.com/docs/9.x/blade#rendering-json – Rodrigo Aug 14 '22 at 05:37

6 Answers6

34

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!

Ahmed Aboud
  • 1,232
  • 16
  • 19
Asim Shahzad
  • 1,438
  • 11
  • 18
11

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

aefxx
  • 24,835
  • 6
  • 45
  • 55
Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
5

add "use Response;"

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

hope this helps!

Gaurav Gupta
  • 1,588
  • 2
  • 14
  • 21
2

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!

STA
  • 30,729
  • 8
  • 45
  • 59
Muaaz Rafi
  • 1,469
  • 2
  • 15
  • 23
1

if we you laravel 5.5 then you should use eloquent-resources

0

in Laravel you can easily serialize your php object like so :

public static function getLocataireByCin($cin)
    {
        return Locataire::where('cin', $cin)
            ->first()
            ->toJson(JSON_PRETTY_PRINT);
    }

and then retrieve it on your frontEnd (using js/jquery for example ) :

$("#cinLocataire").keyup(function () {
        $.ajax({
            url: "{{ route('Caisse.store') }}",
            type: "POST",
            data: {
                cin: $(this).val()
            },
            success: function (locataireData) {
                var locataire = JSON.parse(locataireData); // <-- here
                console.log(
                    locataire.nom
                );
            },
        })
    });
Hicham O-Sfh
  • 731
  • 10
  • 12