Support the ongoing development of Laravel.io →
Requests Database Eloquent
Last updated 1 year ago.
0

Hi BhavinShah2010, try json_encode helper:

json_encode(DB::table('users')->get()->toArray()); // convert any array to json object

, but I advise to make this through Eloquent model User::all() , output of collection automatically will be in JSON format object

Last updated 7 years ago.
0

I you want to use the json data in the controller, use jasonrust's code.

If you want to use it as a json reply, just return the array in your controller method. The view will take care of the rest.

0

Hello, jasonrust

You mean to say like this ,

return json_encode(User:all());

Thank You.

0

LaurentMeganck , Yes.

I have to pass the json format data to somewhere using API. so can I use json array instead of json Object?

0

BhavinShah2010 said:

Hello, jasonrust

You mean to say like this ,

return json_encode(User:all());

Thank You.

If u need json as string I advise to use ->toJson() method on collection, example:

User::all()->toJson();
Last updated 7 years ago.
0

Hello jsonrust, I want data in JSON Object and then want to send JSON Object to somewhere using API.

Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.