Skip to content

Instantly share code, notes, and snippets.

@mkhlil1288
Created February 12, 2017 14:45
Show Gist options
  • Save mkhlil1288/87461632de0c7f250998703ba52421c6 to your computer and use it in GitHub Desktop.
Save mkhlil1288/87461632de0c7f250998703ba52421c6 to your computer and use it in GitHub Desktop.
Nested Json laravel
        public function getModelsbymake()
        	{
                try{
                    $statusCode = 200;
                    $response = [
                      'make'  => []
                    //   'models'  => []
                    ];

                    $models = Mod::all();
                    foreach($models as $model){
                        $response['models'][] = [
                            'id' => $model->id,
                            'make_name' => $model->mod_name,
                            'ads' => Ads::where('model_id', $model->id)->get()
                        ];
                    }

                    $makes = Make::all();
                    foreach($makes as $make){
                        $response['make'][] = [
                            'id' => $make->id,
                            'make_name' => $make->make_name,
                            'image' => $make->image,
                            'models' => $response['models']
                        ];
                    }

                }catch (Exception $e){
                    $statusCode = 400;
                }
                    return Response::json($response, $statusCode);
        	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment