Reputation: 1
i want create dynamic combobox from database, but i have an error with this message :
ErrorException in CrudTrait.php line 32: Undefined offset: 0 (View: /home/vagrant/Code/hrd/resources/views/vendor/backpack/crud/fields/select2.blade.php) (View: /home/vagrant/Code/hrd/resources/views/vendor/backpack/crud/fields/select2.blade.php) (View: /home/vagrant/Code/hrd/resources/views/vendor/backpack/crud/fields/select2.blade.php)
This is my 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();
}
}
The code is run well if i remove :
$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
]);
I use Backpack for Laravel 5.2
Upvotes: 0
Views: 780
Reputation: 1071
Not sure if it will solve, but i can only see two issues that might be causing that.
You don't have cities relationship defined or you could try using:
public function setUp()
{
$this->crud->setModel("App\Models\District");
$this->crud->setRoute("admin/district");
$this->crud->setEntityNameStrings('district', 'districts');
$this->crud->setFromDb();
...
...
don't use parent::__construct().
Give it a try and post your results if not solved.
Upvotes: 0