Search code examples
phpjsonlaravellaravel-5laravel-collection

Laravel collection - flatten is not working


I have student database in which I have only 3 columns (id, name, dob). I've written simple select query,

return DB::table('student')->get(['id','name','dob']);

I'm getting response,

[{"id":1,"name":"Kaylah Hayes","dob":"1993-02-24"},{"id":2,"name":"Janis Casper Sr.","dob":"1994-07-11"}]

But I only need values like this,

[{1,"Kaylah Hayes","1993-02-24"}, {2,"Janis Casper Sr.","1994-07-11"}]

I tried using flatten method,

return DB::table('student')->get(['id','name','dob'])->flatten();

but it's not working.

Thanks.


Solution

  • You could try something like:

    DB::table('student')->get(['id', 'name', 'dob'])->map(function ($item) {
        return collect($item)->values();
    });
    

    Hope this helps!