2016-09-15 106 views
0

我想创建从数据库动态组合框,但是我有一个错误此消息:ErrorException在CrudTrait.php未定义偏移量:0背包Laravel 5.2

ErrorException在CrudTrait.php线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 
          ]); 

我用背包Laravel 5.2

+0

你好。为了调试这个,你可以去CrudTrait.php:32并复制粘贴代码吗?另外,你确定你的地区模特有“城市”关系吗?干杯! – tabacitu

回答

0

不知道是否会解决,但我只可以看到,可能会导致这两个问题。

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

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

不使用父:: __构造()。

试试看,如果不解决,请发布您的结果。