Reputation: 1523
I'm using JSON request to get nation detail in my view, here is how I am doing it; Controller:
public function ajaxrequest(Request $request)
{
$nations = Nation::all()->pluck('nation', 'id');
return response()->json($nations);
}
now I want to access data from Area table, I will have to create another controller? or I can add that in the above controller? I have 10 different tables like nation from where I want to get data through JSON. but I am not sure whether I can do everything in a single controller.
Upvotes: 0
Views: 102
Reputation: 156
Like @ViperTecPro mentioned, you can access multiple tables from the same method in a controller but if possible you should have separate endpoints for each case to you get rid of multiple if checks. Just a thought.
Upvotes: 1
Reputation: 3274
It all depends how do you want to access data and yes you can fetch data from one controller only if it's needed.
Also you can check it based on the request
EXAMPLE :
public function ajaxrequest(Request $request)
{
$check = $request->get('something_to_check");
if($check){
$data = Table1::all()->pluck('id');
}else{
$data = Table2::all()->pluck('id');
}
return response()->json([
'data' => $data,
//...
]);
}
Upvotes: 1