Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eggplantSword's avatar

Use api with a controller

How can I return info from a controller to an api route, right now I'm doing all the login inside the api route itself but this looks messy I'm wondering if I could do this from a controller and just return the info from there?

This is what I have right now

Route::middleware('auth:sanctum')->get('/surveys', function (Request $request) {

    $now = Carbon::now();
    $user = $request->user()->id;

    $evaluatorGroup = EvaluationEvaluator::with('evaluation', 'evaluation.survey')
        ->whereHas('evaluation', function ($query) use ($now) {
            $query->whereDate('init_date', '<=', $now);
            $query->whereDate('end_date', '>=', $now);
        })
        ->whereHas('group.users', function ($query) use ($user) {
            $query->where('user_id', $user);
        })
        ->orderBy('updated_at', 'desc')
        ->get();

    $evaluatorUser = EvaluationEvaluator::with('evaluation.survey', 'evaluation')
        ->whereHas('evaluation', function ($query) use ($now) {
            $query->whereDate('init_date', '<=', $now);
            $query->whereDate('end_date', '>=', $now);
        })
        ->where('user_id', $user)
        ->get();

    foreach ($evaluatorUser as $eU) {
        $eid = Evaluation::where('id', $eU->evaluation_id)->pluck('survey_id')->first();
        $answered = SurveyAnswer::where('survey_id', $eid)->where('user_id', auth()->user()->id)->count();
        if ($answered > 0) {
            $eU['answered'] = true;
        } else {
            $eU['answered'] = false;
        }
    }

    foreach ($evaluatorGroup as $eG) {
        $eid = Evaluation::where('id', $eG->evaluation_id)->pluck('survey_id')->first();
        $answered = SurveyAnswer::where('survey_id', $eid)->where('user_id', auth()->user()->id)->count();
        if ($answered > 0) {
            $eG['answered'] = true;
        }  else {
            $eG['answered'] = false;
        }
    }

    $surveys['group'] = $evaluatorGroup;
    $surveys['single'] = $evaluatorUser;


    return $surveys;
});

This is the response, which isn't ideal honestly but I don't know how to fix it

{
  "group": [
    {
      "id": 2,
      "evaluation_id": 1,
      "user_id": null,
      "group_id": 1,
      "created_at": "2020-04-20 11:04:31",
      "updated_at": "2020-04-20 11:04:31",
      "answered": false,
      "evaluation": {
        "id": 1,
        "survey_id": 1,
        "init_date": "2020-04-13 12:00:00",
        "end_date": "2020-04-30 12:00:00",
        "report": 0,
        "report_date": null,
        "report_end": null,
        "created_at": "2020-04-20 11:04:21",
        "updated_at": "2020-04-20 11:04:21",
        "survey": {
          "id": 1,
          "name": "asdfasdfds group",
          "description": "<p>adfadsfa<strong><em>dsf<\/em><\/strong>asdfadsf asd asdf f sad fsd<\/p>",
          "user_id": 1,
          "anonymous": 0,
          "created_at": "2020-04-20 11:04:21",
          "updated_at": "2020-04-20 11:04:31"
        }
      }
    }
  ],
  "single": [
    {
      "id": 5,
      "evaluation_id": 2,
      "user_id": 1,
      "group_id": null,
      "created_at": "2020-04-21 11:00:19",
      "updated_at": "2020-04-21 11:00:19",
      "answered": true,
      "evaluation": {
        "id": 2,
        "survey_id": 2,
        "init_date": "2020-04-13 12:00:00",
        "end_date": "2020-04-24 12:00:00",
        "report": 0,
        "report_date": null,
        "report_end": null,
        "created_at": "2020-04-20 11:05:43",
        "updated_at": "2020-04-20 11:05:43",
        "survey": {
          "id": 2,
          "name": "asdfa single",
          "description": "<p>asdfa<\/p>",
          "user_id": 1,
          "anonymous": 1,
          "created_at": "2020-04-20 11:05:43",
          "updated_at": "2020-04-21 11:00:19"
        }
      }
    }
  ]
}

This is what I would like to get instead

[
   {
      "id":2,
      "evaluation_id":1,
      "user_id":null,
      "group_id":1,
      "created_at":"2020-04-20 11:04:31",
      "updated_at":"2020-04-20 11:04:31",
      "answered":false,
      "evaluation":{
         "id":1,
         "survey_id":1,
         "init_date":"2020-04-13 12:00:00",
         "end_date":"2020-04-30 12:00:00",
         "report":0,
         "report_date":null,
         "report_end":null,
         "created_at":"2020-04-20 11:04:21",
         "updated_at":"2020-04-20 11:04:21",
         "survey":{
            "id":1,
            "name":"asdfasdfds group",
            "description":"<p>adfadsfa<strong><em>dsf<\/em><\/strong>asdfadsf asd asdf f sad fsd<\/p>",
            "user_id":1,
            "anonymous":0,
            "created_at":"2020-04-20 11:04:21",
            "updated_at":"2020-04-20 11:04:31"
         }
      }
   },
   {
      "id":5,
      "evaluation_id":2,
      "user_id":1,
      "group_id":null,
      "created_at":"2020-04-21 11:00:19",
      "updated_at":"2020-04-21 11:00:19",
      "answered":true,
      "evaluation":{
         "id":2,
         "survey_id":2,
         "init_date":"2020-04-13 12:00:00",
         "end_date":"2020-04-24 12:00:00",
         "report":0,
         "report_date":null,
         "report_end":null,
         "created_at":"2020-04-20 11:05:43",
         "updated_at":"2020-04-20 11:05:43",
         "survey":{
            "id":2,
            "name":"asdfa single",
            "description":"<p>asdfa<\/p>",
            "user_id":1,
            "anonymous":1,
            "created_at":"2020-04-20 11:05:43",
            "updated_at":"2020-04-21 11:00:19"
         }
      }
   }
]

How can I do all the login in a controller and also get the correct response?

0 likes
1 reply
danwah's avatar
danwah
Best Answer
Level 18

For the first part of your question you could do a few things, but what i tend to do especially with API endpoints is to use a Route group like so:

Route::group(['middleware' => ['auth:sanctum'] ], function() {

    // Surveys
    Route::apiResource('/surveys', 'SurveyController', ['only'=> ['index','show','update','store','destroy']]);

});

And to create the controller you can do php artisan make:controller SurverController --api --model=Evaluation

passing in the --api flag gives you methods to handle the GET, POST, PATCH and DELETE methods.

If you don't want to use an apiResource, you can just as easily do it this way too:

Route::group(['middleware' => ['auth:sanctum'] ], function() {

    // Surveys
    Route::get('/surveys', 'SurveyController@survey');

});

And just create a public function called survey in the controller like so:

    public function surveys(Request $request)
    {
        // Copy your code from the route into here
    }

Hope this helps!

Please or to participate in this conversation.