4

I've made a custom page in backpack admin panel. This page is non-CRUD (not related to any model). There are several forms on it, with date pickers, select inputs, etc. So I'am trying to find a way to use backpack fields to create these date pickers and select inputs. Because it seems to be awkward to embed custom js-controls into the project, as Backpack already has appropriate fields.

The only solution I came up with, is to create a crud controller for random model, disable all operations except create, use create operation view as custom page (backpack fields are available this way), and finally override store() method - to prevent creating new model entry in DB.

So, is there a proper way to access backpack fields on custom (non-CRUD) page?

Mikhail
  • 65
  • 6

1 Answers1

3

Backpack 4.x fields aren't meant to be used outside CRUDs, but you can do that.

Option A

At their core, Backpack fields are just Blade views, so you can load them using the Blade helper @include(). Just make sure to pass along all variables that the blade file needs. I believe in 99% of the fields that will be a $field and a $crud variable, so this will work:

    @php
        // set the CRUD model to something (anything)
        // but ideally it'd be the model of the entity that has the form
        $crud = app()->make('crud');
        $crud->setModel(\App\Models\Monster::class);
    @endphp

    @include('crud::fields.number', [
        'crud' => $crud,
        'field' => [
            'name' => 'price',
            'label' => 'Price',
            'prefix' => '$'
        ]
    ])

This way, you only load the bits you actually want (the inputs), without the overhead of a CrudController. You can point the form to your custom controller and do the saving yourself. What you need to pass for a $field above is a Backpack field definition in array form.

This way is super-simple, but it has a big downside if you ask me. The field definition has to be 100% correct and complete, you lose all the magic and assumption logic that Backpack usually does to make your life easier when you add field using addField(). That's why in most cases I think it's more convenient to go with Option B.

Option B

Instead of manually loading all each field Blade view, add them using addField(), then load all of them just like Backpack does it in the Create or Update operation:

    @php
        $crud = app()->make('crud');
        $crud->setModel(\App\Models\Monster::class);
        $crud->addField([
            'name' => 'price',
            'label' => 'Price',
            'prefix' => '$'
        ]);
    @endphp

    <form method="post">
        @include('crud::form_content', [ 'fields' => $crud->fields(), 'action' => 'create' ])
    </form>

The above will produce an output like this: enter image description here

The benefit of this second option is that you can "forget" to mention stuff in the field definition and Backpack will assume it, you can use the fluent syntax, you can use most Backpack features, really...

tabacitu
  • 6,047
  • 1
  • 23
  • 37
  • I have included them, now I have a problem that they are unstyled. How can I fix that? I have checked - I use the same structure as for backpack own pages. Still no luck :/ – naneri Jan 27 '22 at 19:04