CrudTrait.php中的ErrorException未定义的偏移量:0为Laravel 5.2提供背包

时间:2016-09-15 07:31:33

标签: laravel backpack-for-laravel

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

CrudTrait.php中的ErrorException第32行:未定义的偏移量: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
                            ]);   

我使用Backpack for Laravel 5.2

1 个答案:

答案 0 :(得分: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()。

试一试,如果没有解决,请发布结果。