简体   繁体   中英

Laravel Backpack checklist array storing ids instead of name values

I made a checklist with the possible sizes of a certain product but its storing the ids instead of the specific size.

This is the size field display.

I have the size field code like this:

CRUD::addField([
        'label'     => 'Size',
        'type'      => 'checklist',
        'name'      => 'size',
        'entity'    => 'sizes',
        'attribute' => 'name',
        'model'     => 'App\Models\Size',
        'pivot'     => false,
    ]);

How do I change it to store the size's name?

I solved this by creating a pivot table between merch and sizes and then changing the type of the column size.

$this->crud->addColumns( [
        [
            'label' => 'Sizes',
            'type' => "select_multiple",
            'entity' => 'sizes', // the method that defines the relationship in your Model
            'name' => 'sizes',
            'attribute' => "name", // foreign key attribute that is shown to user
            'model' => "App\Models\Size",
            'pivot'     => true,
        ],
    ]);

You should be able to give an array to the “options” attribute, instead of specifying “entity”, “attribute”, “model”, “pivot”.

The array elements should be [value => label], where the value is what will be stored in the db, the label is what is shown next to the checkbox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM