0

Here is my Row Query to get the data from database.

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

I want to pass my $users array data to json format. How can I do so?

I have tried using Eloquent Model. but didn't work.

Thank You in Advance.

3
  • 4
    Um, did you try json_encode? Commented Jun 20, 2016 at 12:07
  • yes... I have tried. i m getting whole array values of database in json_encode(). Thank you.. Commented Jun 20, 2016 at 13:24
  • @Jeff Puckett , I can use Eloquent Model to get data in JSON Object return json_encode(User:all()); Commented Jun 21, 2016 at 7:51

3 Answers 3

1

If you use Eloquent then you can use ->toJson();

But you are using Query builder. So you have to use php function: json_encode()

For more information see here http://php.net/manual/en/function.json-encode.php

Sign up to request clarification or add additional context in comments.

2 Comments

Yes i m using Query Builder. Thanks
Bhavin, if my answer is helpful then please accept it :)
0

Eloquent has a method named

->toJson()

However you're code doesn't looks like Eloquent.

1 Comment

I m using Query Builder. so using json_encode() , i can get array values in json format.
0

To get an array instead of object, you have to pass true as second parameter

json_decode($user,true)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.