简体   繁体   中英

BackPack for Laravel: Display two fields with same name

What I have:

Tables in DB:

Schema::create('orders', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('primary_person');
    $table->unsignedBigInteger('secondary_person');
});
Schema::create('persons', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('first_name', 255);
    $table->string('last_name', 255);
});

Model:

class Order extends Model
{
    use CrudTrait;

    protected $table = 'orders';
    protected $fillable = [
        'primary_person',
        'secondary_person',
    ];

    public function primaryPerson()
    {
        return $this->hasOne(Person::class, 'id', 'primary_person');
    }

    public function secondaryPerson()
    {
        return $this->hasOne(Person::class, 'id', 'secondary_person');
    }
}

What need to do:

On page edit order I want to display information about two persons:

  • First name of Primary person
  • Last name of Primary person
  • First name of Secondary person
  • Last name of Secondary person

What I did:

In OrderCrudController.php added:

$this->crud->addFields([
    [
       'label' => 'First Name of Primary person',
       'type' => 'text',
       'name' => 'first_name',
       'entity' => 'primaryPerson',
       'model' => 'App\Models\Person',
    ],
    [
        'label' => 'Last Name of Primary person',
        'type' => 'text',
        'name' => 'last_name',
        'entity' => 'primaryPerson',
        'model' => 'App\Models\Person',
    ],
    [
        'label' => 'First Name of Secondary person',
        'type' => 'text',
        'name' => 'first_name',
        'entity' => 'secondaryPerson',
        'model' => 'App\Models\Person',
    ],
    [
        'label' => 'Last Name of Secondary person',
        'type' => 'text',
        'name' => 'last_name',
        'entity' => 'secondaryPerson',
        'model' => 'App\Models\Person',
    ]
]);

What I receive:

Only latest two fields (secondary person information).

在此处输入图片说明

What I want to see

在此处输入图片说明

The main problem:

I couldn't display Fields with the same "name" , because "name" - the attribute (column in the database), which will also become the name of the input.
In Columns this situation handled with "key" attribute. But in Fields it doesn't work.

Possible solution – use accessors and mutators (many thanks to @tabacitu).
https://dev.to/sergunik/how-to-display-two-fields-with-same-name-in-backpack-for-laravel-8b1

Indeed, you cannot have two fields with the same name. In columns it's possible using the key attribute, but as you've mentioned, in the create/update forms it's a problem because of how HTML forms work: only the last input's value will be passed to PHP, to be stored in the database.

I recommend you work around this using accessors & mutators . You can name the second field something else, even if that column does not exist in the database. Then just have a ```setSecondFirstName($value)```` method inside your Model that takes that value and saves it however you want.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM