Php 如何将现有值指定给select?

Php 如何将现有值指定给select?,php,laravel,backpack-for-laravel,Php,Laravel,Backpack For Laravel,我将区域作为服务器的查找表。 在表中列出保存的条目时没有问题。 但是,当我编辑条目时,字段不会预先选择保存的值。我如何设置它 -- Table -- Schema::create('servers', function (Blueprint $table) { $table->increments('id'); $table->string('name')->unique(); $table->integer('region_id')->u

我将区域作为服务器的查找表。 在表中列出保存的条目时没有问题。 但是,当我编辑条目时,字段不会预先选择保存的值。我如何设置它

-- 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',
  ]);
}

尝试默认属性,例如

$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',
 ]);

希望这有帮助。

将正确的hasOne和belongs放置到模型中修复了该问题

-- 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',
]);

如果我将类型更改为“select”,如何从DB中获取赋值“default”值