Return multiple array to Response::json laravel?

44,135

i think you can try this method:

$user= User::all()->toArray();
$post= Post::all()->toArray();
$comment= Comment:all()->toArray();

Response::json(['user'=>$user,'post'=>$post,'comment'=>$comment]);
Share:
44,135
Anil Sharma
Author by

Anil Sharma

A tech geek who loves exploring web . Love to explore new modern evolving technologies and dig into Linux administration . Have working experience on below stacks : AngularJS | Material | Laravel | Jquery | NodeJS | Ionic | Cordova | MongoDB | Mysql | Html5 | CSS3 | Development tools (GULP | BOWER) Innovation : key to Success

Updated on July 05, 2022

Comments

  • Anil Sharma
    Anil Sharma almost 2 years

    How we can return multiple array in json. Suppose we get the following response in Laravel eloquent:

    $user= User::all();
    $post= Post::all();
    $comment= Comment:all();
    

    Now I want to return response in json which include these data:

    Response::json(array('user'=>$user,'post'=>$post,'comment'=>$comment));
    

    Using the above method empty value is returned. Any help would be appreciated

    Sorry guys. I found the solution. The data that I was passing was already in object form. Therefore I needed to convert it into an array and then pass it.

    $user= User::all()->toArray();
    $post= Post::all()->toArray();
    $comment= Comment:all()->toArray();
    

    Now it will work!