0

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.

2 Answers 2

1

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,
       //...
    ]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

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.