Override CRUD views
Asked Answered
F

4

8

I would like to override the CRUD views of the Laravel Backpack CRUD package, because I want to make small changes to the layout. But obviously I don't want to change the CRUD package itself. Is there an elegant to do this?

Favela answered 2/9, 2016 at 10:50 Comment(0)
B
6

Before loading any views, Backpack for Laravel checks your resources/views/vendor/backpack/crud folder to see if you have any custom views. If you don't, it will just load the views in the package.

If you want to overwrite a blade file for all CRUDS, you can just place a file with the right name in the right folder. Take a look at how the files are organized in the package.

If you want to overwrite a blade file for just one CRUD, use Sachin's solution.

Bilestone answered 2/9, 2016 at 15:54 Comment(1)
1. How to customize/overwrite form_save_buttons just one controller, I am trying to remove save drop down. 2. How can I add 'CRUD::removeSaveAction('save_and_edit');' globally/config to affect all the controllerShastashastra
D
11

In your controller which is extending Backpack\CRUD\app\Http\Controllers\CrudController you need to override the method like index,create,edit which you want to change. All method are in-

Backpack\CRUD\app\Http\Controllers\CrudController

All the methods are here. You need to change here

 public function index()
{
    $this->crud->hasAccessOrFail('list');

    $this->data['crud'] = $this->crud;
    $this->data['title'] = ucfirst($this->crud->entity_name_plural);

    // get all entries if AJAX is not enabled
    if (! $this->data['crud']->ajaxTable()) {
        $this->data['entries'] = $this->data['crud']->getEntries();
    }

    // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
    // $this->crud->getListView() returns 'list' by default, or 'list_ajax' if ajax was enabled
    return view('your_view_name', $this->data);
}
Discophile answered 2/9, 2016 at 12:38 Comment(2)
Can you comment on the filename and path you are creating? If this was to update a book model record, would the the blade template be in /resources/views/vendor/backpack/crud/book/edit.blade.php? I haven't quite worked it out.Enclose
I worked it out. In my case I wanted to edit a course record so I created a file in /resources/views/course/edit.blade.php and called this with view('course.edit').Enclose
S
9

Found a way not even having to override the index() method, just use $this->crud->setListView() in your setup method of your CrudController, ex:

$this->crud->setListView('backpack::crud.different_list', $this->data);

thus, it will get the view under '/resources/views/vendor/backpack/crud/different_list.blade.php' instead of the default one in the package.

Besides setListView(), setEditView(), setCreateView(), setUpdateView()....are also available. hope it helps.

you can refer to https://laravel-backpack.readme.io/docs/crud-full-api for more detail.

// use a custom view for a CRUD operation
$this->crud->setShowView('your-view');
$this->crud->setEditView('your-view');
$this->crud->setCreateView('your-view');
$this->crud->setListView('your-view');
$this->crud->setReorderView('your-view');
$this->crud->setRevisionsView('your-view');
$this->crud->setRevisionsTimelineView('your-view');
$this->crud->setDetailsRowView('your-view');
Shool answered 18/9, 2017 at 4:41 Comment(0)
B
6

Before loading any views, Backpack for Laravel checks your resources/views/vendor/backpack/crud folder to see if you have any custom views. If you don't, it will just load the views in the package.

If you want to overwrite a blade file for all CRUDS, you can just place a file with the right name in the right folder. Take a look at how the files are organized in the package.

If you want to overwrite a blade file for just one CRUD, use Sachin's solution.

Bilestone answered 2/9, 2016 at 15:54 Comment(1)
1. How to customize/overwrite form_save_buttons just one controller, I am trying to remove save drop down. 2. How can I add 'CRUD::removeSaveAction('save_and_edit');' globally/config to affect all the controllerShastashastra
W
1

There's actually a better way for newer versions of backpack (^version 5x). Copied directly from the documentation: Overriding Show Operation

In case you need to modify the show logic in a meaningful way, you can create a show() method in your EntityCrudController. The route will then point to your method, instead of the one in the trait. For example:

use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation { show as traitShow; }

public function show($id)
{
    // custom logic before
    $content = $this->traitShow($id);
    // cutom logic after
    return $content;
}

Then just do your regular laravel stuff for example, return a view:

return view('entity.show', ['data' => $data]);
Wiseacre answered 7/9, 2022 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.