Home > Back-end >  How to select a specific method from a class in Route::resource?
How to select a specific method from a class in Route::resource?

Time:10-07

I'am building my first laravel application. And I have a question that I couldn't find anywhere else..

I have this block of code :

Route::prefix('/api')->group(function () {
        Route::resource('/buildings', BuildingController::class);
})

Does anyone know how to select a specific method in resource the BuildingController. in the Route::get I'm using:

Route::get("/test", [Controller::class, "exampleMethod"])

But when I use that in a Route::resource it returns "Array to String conversion"

Any solutions?

Thanks in advance

CodePudding user response:

Yes, there is a solution, u don't have to add the method u want to use because the laravel is very smart. For example:

If u have a resource point ../api/buildings/1/edit the Route::resource knows that u want to edit the building with id 1

So u only need:

Route::prefix('/api')->group(function () {
        Route::resource('/buildings', BuildingController::class);
})

and in ur controller u need to create a method 'show' and that method will be called when u go to ../api/buildings/1

you can get the default methods by creating a controller through artisan:

$ php artisan make:controller NameOfUrController

Examples : https://github.com/artesaos/laravel-docs/blob/master/controllers.md

  • Related