Add a function in Laravel Route Resource

Asked

Viewed 59 times

-1

I’m new to Laravel, I have a question, I want to implement a function beyond what already exists in the part of Resource.

In Resource it looks like this

Route::resources([
    'users' => UserController::class,
    'placas' => PlacaController::class,
]);

I just don’t want to use the Destroy method, so I created a Delete method, but in order to use this method, I had to choose to write more lines

Route::prefix('placas')->name('placas.')->group(function(){
    Route::get('', [PlacaController::class, 'index'])->name('index');
    Route::get('create', [PlacaController::class, 'create'])->name('create');
    Route::post('store', [PlacaController::class, 'store'])->name('store');
    Route::get('{placa}/edit', [PlacaController::class, 'edit'])->name('edit');
    Route::put('update/{placa}', [PlacaController::class, 'update'])->name('update');
    Route::post('/delete', [PlacaControlleroller::class, 'delete'])->name('delete');
});

Is there any way I can add only delete on the board part in Resource

1 answer

0

all right?

As you may already know, the Laravel Resource router enables a standard CRUD, following a model determined by the Laravel developers themselves. Hence the option to name the method as detroy() and not as delete() as you would like.

It is possible to remove only the Destroy() from the CRUD by chaining, or the method only() or the method except(). In your case, it should look something like this:

Route::resources([
    'users' => UserController::class,
    'placas' => PlacaController::class,
])->except(['destroy']);

That is described in this part of the documentation.

I hope it helps you!

Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.