Home > Software engineering >  Laravel API routes and Controller variable
Laravel API routes and Controller variable

Time:11-27

I'm a new user of Laravel, and i'm a bit confused with Laravel route API and the name of variable in the controller. Here an example to explain : An API route

Route::middleware('auth:sanctum')->group( function () {
    Route::resource('cepage', CepageController::class);
});

For a PUT or PATCH, i have this function in the CepageController :

public function update(Request $request, Cepage $cepage)
    {
        $input = $request->all();

        $validator = Validator::make($input, [
            'libelle' => 'required',
            'abrege' => 'required'
        ]);

        if($validator->fails()){
            return $this->sendError($validator->errors());
        }

        $cepage->libelle = $input['libelle'];
        $cepage->abrege = $input['abrege'];
        $cepage->save();

        return $this->sendResponse(new CepageResource($cepage), 'Cépage mis à jour');
    }

If you see my route name "cepage" have the same name of the $cepage variable of the function declaration in the controller, Laravel update the record in the database.

If they are no identical, Laravel create a new record in the database.

Why they need to be exactly the same ?

I think i miss something in the documenation of Laravel.

Thanks for your explanations.

CodePudding user response:

Thanks i understand better, but now i have a problem with User and AuthController.

My route :

Route::middleware('auth:sanctum')->group( function () {
    Route::resource('evvoperateurs', AuthController::class);
});

And in the update function i use :

public function update(Request $request, $evvoperateurs_id)

But $evvoperateurs is empty, i need to use this to get the good user

public function update(Request $request, $cepage_id)
{
   $evvoperateurs = User::find($evvoperateurs_id);
   //here you have to fetch the object for yourself to access it
}

Do you have any idea why it's empty only with this case ?

CodePudding user response:

It needs to be the same, for laravel to know what object does he needs to create for us.

Route::resource does a few routes for you, with the base url give into it (https://laravel.com/docs/8.x/controllers#actions-handled-by-resource-controller)

So once you have defined Route::resource('cepage', CepageController::class) You will have the following routes defined:

  • Verb URI Action Route Name

  • GET /cepage CepageController@index cepage.index

  • GET /cepage/create CepageController@create cepage.create

  • POST /cepage CepageController@store cepage.store

  • GET /cepage/{cepage_id} CepageController@show cepage.show

  • GET /cepage/{cepage_id}/edit CepageController@edit cepage.edit

  • PUT/PATCH /cepage/{cepage_id} CepageController@update cepage.update

  • DELETE /cepage/{cepage_id} CepageController@destroy cepage.destroy

And in the controller you need to follow the naming, because in the url you have only ids of the object. But if you follow the naming, laravel will fetch the object for you by its id. See:

public function update(Request $request, $cepage_id)
{
   $cepage = Cepage::find($cepage_id);
   //here you have to fetch the object for yourself to access it
}
public function update(Request $request, Cepage $cepage)
{
   //here you can already access $cepage variable
}
  • Related