laravel 如何在更新动作背包上显示关系数据?

eyh26e7m  于 2023-06-24  发布在  其他
关注(0)|答案(2)|浏览(130)

在Backpack上为Laravel做CRUD,遇到了问题。我有Domain和domainPrices表Domain model:

public function domainPrice()
{
    return $this->hasOne(DomainPrices::class, 'domain_id', 'id');
}

DomainPrices模型:

public function domain()
{
    return $this->belongsTo(Domain::class, 'id', 'domain_id');
}

迁移:

Schema::table('domain_prices', function(Blueprint $table) {
        $table->foreign('domain_id')->references('id')->on('domains')
                    ->onDelete('cascade')
                    ->onUpdate('cascade');
    });

对于setupCreateOperation()

$this->crud->addField([
    'name' => 'bin_price',
    'type' => 'number',
    'entity' => 'domainPrice',
    'attribute' => 'bin_price',
    'model' => 'DomainPrices',
    'pivot' => false,
]);

setupUpdateOperation:

$this->setupCreateOperation();

我可以用这个来存储数据,但是当我使用更新路由时,有一个空字段。但它是显示值,在列表视图与:

CRUD::column('domainPrice')->attribute('bin_price');

PHP版本:简体中文
LARAVEL版本:v8.44.0
背包版本:4.1.46

4jb9z9bj

4jb9z9bj1#

解决方案比我想象的要容易,而不是:

$this->crud->addField([
        'name' => 'bin_price',
        'type' => 'number',
        'entity' => 'domainPrice',
        'attribute' => 'bin_price',
        'model' => 'DomainPrices',
        'pivot' => false,
    ]);

我用途:

$this->crud->field('domainPrice.bin_price');
jv4diomz

jv4diomz2#

扩展解决方案,以防您尝试从父创建/编辑表单创建/更新1:1子记录:
父型号:

public function child(): HasOne
{
    return $this->hasOne(Child::class);
}

子模型:

public function parent(): BelongsTo
{
    return $this->belongsTo(Parent::class);
}

父CRUD控制器:

CRUD::field('child')->type('relationship')
    ->label('Child Label')
    ->subfields($arrayOfFields);

确保在子CRUD控制器中导入内联表单trait

use \Backpack\CRUD\app\Http\Controllers\Operations\InlineCreateOperation;
  • (Laravel Backpack 5.x)*

相关问题