2016-09-15 117 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(); 
... 
... 

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

試試看,如果不解決,請發佈您的結果。