Как назначить существующее значение для выбора? - PullRequest
0 голосов
/ 30 ноября 2018

У меня есть Регион в качестве справочной таблицы для Сервер .Нет проблем со списком сохраненных записей в таблице.Однако, когда я редактирую запись, поле предварительно не выбирает сохраненное значение.Как я могу установить его?

-- Table --

Schema::create('servers', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name')->unique();
    $table->integer('region_id')->unsigned();
    $table->timestamps();

Schema::table('servers', function(Blueprint $table) {
    $table->foreign('region_id')->references('id')->on('lookup_regions')->onDelete('restrict')->onUpdate('restrict');
});

Schema::create('lookup_regions', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name')->unique();
    $table->timestamps();
});


-- Model --

class Server extends Model
{
  public function region()
  {
    return $this->hasOne('App\Models\Region', 'id', 'region_id');
  }
}

class Region extends Model
{
  public function server()
  {
    return $this->belongsTo('App\Models\Server', 'id', 'region_id');
  }
}


-- Controller --

class ServerCrudController extends CrudController
{
  $this->crud->addColumn([
    'label' => 'Region',
    'type' => 'select',
    'name' => 'region_id',
    'entity' => 'region',
    'attribute' => 'name',
    'model' => 'App\Models\Region'
  ]);

  $this->crud->addField([
    'label' => 'Region',
    'type' => 'select',
    'name' => 'region_id',
    'entity' => 'region',
    'attribute' => 'name',
    'model' => 'App\Models\Region',
  ]);
}

Ответы [ 2 ]

0 голосов
/ 05 декабря 2018

Размещение правильного hasOne и принадлежит To в модели решило проблему

-- Model --
-- Server.php --

public function provider()
{
    return $this->belongsTo('App\Models\Provider', 'provider_id', 'id');
}

-- Region.php --

public function proxy()
{
    return $this->hasOne('App\Models\Proxy', 'id', 'region_id');
}


-- Controller --
-- ServerCrudController.php --

$this->crud->addField([
    'label' => 'Region',
    'type' => 'select',
    'name' => 'region_id',
    'entity' => 'region',
    'attribute' => 'name',
    'model' => 'App\Models\Region',
]);
0 голосов
/ 30 ноября 2018

Попробуйте атрибут по умолчанию, например,

$this->crud->addColumn([
    'label' => 'Region',
    'type' => 'select_from_array',
    'name' => 'region_id',
    'entity' => 'region',
    'attribute' => 'name',
    'model' => 'App\Models\Region'
    'options'     => [ 
                    'val1' => "value1",
                    'val2' => "value2"
                ],
     'default' => 'val1',
 ]);

Надеюсь, это поможет.

...