Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I add custom routes for CRUD operations in Laravel Backpack
    Laravel Backpack custom routes CRUD operations Laravel admin panel custom operations Laravel Backpack tutorial


How can I add custom routes for CRUD operations in Laravel Backpack


To add custom routes for CRUD operations in Laravel Backpack, you can follow these steps:

1. Create a custom operation: First, you need to create a custom operation for your CRUD. You can do this by running the following Artisan command:


php artisan backpack:crud-operation MyCustomOperation

This will generate a new file `app/Http/Controllers/Admin/Operations/MyCustomOperationOperation.php` that you can customize.

2. Define the custom routes: In the `setupMyCustomOperationRoutes()` method of your custom operation, you can define the routes for your custom operation. For example:

php
protected function setupMyCustomOperationRoutes($segment, $routeName, $controller)
{
    Route::get($segment.'/{id}/my-custom-operation', [
        'as' => $routeName.'.my-custom-operation',
        'uses' => $controller.'@myCustomOperation',
        'operation' => 'my-custom-operation',
    ]);

    Route::post($segment.'/{id}/my-custom-operation', [
        'as' => $routeName.'.save-my-custom-operation',
        'uses' => $controller.'@saveMyCustomOperation',
        'operation' => 'my-custom-operation',
    ]);
}

3. Add the custom operation to your CRUD controller: In your CRUD controller, you need to add the custom operation to the `setupOperations()` method:

php
public function setup()
{
    $this->crud->addOperation('My Custom Operation', 'my-custom-operation', 'App\Http\Controllers\Admin\Operations\MyCustomOperationOperation');
}

4. Create the custom operation methods: Finally, you need to create the `myCustomOperation()` and `saveMyCustomOperation()` methods in your CRUD controller that will handle the custom operation logic.

php
public function myCustomOperation($id)
{
    $this->crud->hasAccessOrFail('my-custom-operation');

    $entry = $this->crud->getEntry($id);

    // Your custom operation logic here

    return view('backpack::crud.my-custom-operation', [
        'entry' => $entry,
        'crud' => $this->crud,
    ]);
}

public function saveMyCustomOperation(Request $request, $id)
{
    $this->crud->hasAccessOrFail('my-custom-operation');

    $entry = $this->crud->getEntry($id);

    // Your custom operation save logic here

    return redirect()->back();
}

By following these steps, you can add custom routes and operations to your Backpack CRUD, allowing you to extend the functionality of your admin panel as needed.

Citations:
[1] https://backpackforlaravel.com/docs/6.x/crud-how-to
[2] https://backpackforlaravel.com/docs/6.x/crud-tutorial
[3] https://backpackforlaravel.com/articles/tutorials/how-to-create-a-custom-operation-with-a-form
[4] https://backpackforlaravel.com/docs/6.x/base-how-to
[5] https://backpackforlaravel.com/docs/6.x/crud-operations