How to: Using a Foreignkey That is Not A Primary Key in Association


These are my tables:

members (id, mbrid, name, type, created, modified)
appearances (id, member_id, created, modified)

How to code my AppearancesTable so that the association with members is linked to mbrid instead of id?

$this->belongsTo('Members', [
            'foreignKey' => 'member_id'

You can use bindingKey( The name of the column in the other table used to match the foreignKey . The default value is the primary key of the other table such as ‘id’ of Users in the above example). Read here:

1 Like