Search code examples
laravelmany-to-manylaravel-backpack

Having multiple columns (foreign keys) of the same n-n relation in Backpackforlaravel


I'm struggling with the following situation:

My entities Session & Registration are related with a many-to-many (n-n) relationship. In Registration, I have two foreign keys, player_id and hotel_id. In the Session CrudController, I want to display the related name(s) of the Player(s) and Hotel(s).

For Player, it worked like this:

CRUD::addColumn([
        // any type of relationship
        'name'         => 'registrations', // name of relationship method in the model
        'type'         => 'relationship',
        'label'        => 'Spieler', // Table column heading
        // OPTIONAL
        'entity'    => 'registrations', // the method that defines the relationship in your Model
        'attribute' => 'player.full_name', // foreign key attribute that is shown to user
        'model'     => App\Models\Registration::class, // foreign key model    
    ]);

So I've continued to add the Hotel like this:

CRUD::addColumn([
        // any type of relationship
        'name'         => 'registrations', // name of relationship method in the model
        'type'         => 'relationship',
        'label'        => 'Hotel', // Table column heading
        // OPTIONAL
        'entity'    => 'registrations', // the method that defines the relationship in your Model
        'attribute' => 'hotel.name', // foreign key attribute that is shown to user
        'model'     => App\Models\Registration::class, // foreign key model    
    ]);

But unfortunately it doesn't work - I don't get an error message, it just keeps showing only the Player column. I think the reason for this is the same value in 'name' - because after commenting the 'name' value in Player, the Hotel information was visible. But I have no idea how to avoid it.

Currently I'm not sure if it's a bug or if I'm doing anything wrong. Would appreciate any kind of support - thanks in advance!


Solution

  • thank you @tabacitu, that works!

    I've added

    'key' => 'hotel'
    

    and it showed up in the table.