0

我想从数据库创建动态组合框,但我有一条错误消息:

CrudTrait.php 第 32 行中的 ErrorException:未定义偏移量:0(查看:/home/vagrant/Code/hrd/resources/views/vendor/backpack/crud/fields/select2.blade.php)(查看:/home/vagrant/代码/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
                            ]);   

我使用 Laravel 5.2 的背包

4

1 回答 1

0

不确定它是否会解决,但我只能看到两个可能导致该问题的问题。

您没有定义城市关系,或者您可以尝试使用:

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 回答