0

データベースから動的コンボボックスを作成したいのですが、次のメッセージでエラーが発生します:

CrudTrait.php 行 32 の ErrorException: 未定義のオフセット: 0 (表示: /home/vagrant/Code/hrd/resources/views/vendor/backpack/crud/fields/select2.blade.php) (表示: /home/vagrant/ Code/hrd/resources/views/vendor/backpack/crud/fields/select2.blade.php) (ビュー: /home/vagrant/Code/hrd/resources/views/vendor/backpack/crud/fields/select2.blade. php)

これは私の DistrictCrudController.php です:

<?php



namespace App\Http\Controllers\Admin;

use Backpack\CRUD\app\Http\Controllers\CrudController;

use App\Http\Requests\DistrictRequest as StoreRequest;

use App\Http\Requests\DistrictRequest as UpdateRequest;

class DistrictCrudController extends CrudController {

    public function __construct() {
        parent::__construct();

        $this->crud->setModel("App\Models\District");
        $this->crud->setRoute("admin/district");
        $this->crud->setEntityNameStrings('district', 'districts');
        $this->crud->setFromDb();

        $this->crud->addField([  
                               'label' => "City",
                               'type' => 'select2',// the method that defines the relationship in your Model
                               'name' => 'city_id', // the db column for the foreign key
                               'entity' => 'cities', //tabel entity
                               'attribute' => 'id', // foreign key attribute that is shown to user
                               'model' => "App\Models\City" // foreign key model
                            ]);                   
    }

    public function store(StoreRequest $request)
    {
        return parent::storeCrud();
    }

    public function update(UpdateRequest $request)
    {
        return parent::updateCrud();
    }
}

削除すると、コードはうまく実行されます:

$this->crud->addField([  
                               'label' => "City",
                               'type' => 'select2',// the method that defines the relationship in your Model
                               'name' => 'city_id', // the db column for the foreign key
                               'entity' => 'cities', //tabel entity
                               'attribute' => 'id', // foreign key attribute that is shown to user
                               'model' => "App\Models\City" // foreign key model
                            ]);   

バックパック for Laravel 5.2 を使用しています

4

1 に答える 1

0

解決するかどうかはわかりませんが、その原因となっている可能性のある問題は 2 つしかありません。

都市の関係が定義されていないか、次を使用してみてください。

public function setUp()
    {
$this->crud->setModel("App\Models\District");
        $this->crud->setRoute("admin/district");
        $this->crud->setEntityNameStrings('district', 'districts');
        $this->crud->setFromDb();
...
...

parent::__construct() を使用しないでください。

試してみて、解決しない場合は結果を投稿してください。

于 2017-01-25T10:34:55.920 に答える