mrTall
mrTall

Reputation: 51

How to disaplay relation data on update action backpack?

Making CRUD on Backpack for Laravel, and got issue. I have Domain and domainPrices table Domain model:

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

DomainPrices model:

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

Migration:

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

For setupCreateOperation()

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

setupUpdateOperation:

$this->setupCreateOperation();

And I'm able to store data with this, but when I use update route, there is empty field. But it is display the value, on list view with:

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

enter image description here

PHP VERSION: PHP 8.0.5

LARAVEL VERSION: v8.44.0

BACKPACK VERSION: 4.1.46

Upvotes: 1

Views: 159

Answers (2)

QSoto
QSoto

Reputation: 338

Extending the solution in case you are trying to create/update a 1:1 child record, from the parent create/edit form:

Parent Model:

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

Child Model:

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

Parent CRUD Controller:

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

Make sure, you import the inline form trait in the child crud controller

use \Backpack\CRUD\app\Http\Controllers\Operations\InlineCreateOperation;

(Laravel Backpack 5.x)

Upvotes: 0

mrTall
mrTall

Reputation: 51

Solution was more easy than I though Instead of:

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

I use:

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

Upvotes: 1

Related Questions