F
Filament6mo ago
taz

Unconventional Pivot table name. How to tell the Relation manager to use this table name ?

I have a many to many relationship between my Customer Model and Chatter Model and a Pivot class unconventionaly named ChattingInstance. I created a RelationManager for the CustomerResource named ChattersRelationManager but the DB request failed because it tries to use the conventional table name for the Pivot class which should be name chatter_customer. How I can force to use the right table name which is 'chatting_instances' ?
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mydb.chatter_customer' doesn't exist
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mydb.chatter_customer' doesn't exist
class ChattingInstance extends Pivot {
protected $table = 'chatting_instances';
}
class ChattingInstance extends Pivot {
protected $table = 'chatting_instances';
}
class Customer extends Model {
public function chatters(): BelongsToMany
{
return $this->belongsToMany(Chatter::class)
->withPivot('url', 'status')
->withTimestamps()
->using(ChattingInstance::class);
}
}
class Customer extends Model {
public function chatters(): BelongsToMany
{
return $this->belongsToMany(Chatter::class)
->withPivot('url', 'status')
->withTimestamps()
->using(ChattingInstance::class);
}
}
class Chatter extends Model {
public function customers(): BelongsToMany
{
return $this->belongsToMany(Customer::class)
->withPivot('url', 'status')
->withTimestamps()
->using(ChattingInstance::class);
}
}
class Chatter extends Model {
public function customers(): BelongsToMany
{
return $this->belongsToMany(Customer::class)
->withPivot('url', 'status')
->withTimestamps()
->using(ChattingInstance::class);
}
}
4 Replies
nanopanda
nanopanda6mo ago
@taz there are two static strings "$relationship" and "$inverseRelationship" where you can set custom relationship names for the RelationManager
taz
tazOP6mo ago
Thanks you for your anwser. I already tried that. But for the relationship and inverseRelationship the naming is normal, so the DB request is good. The only part of the request that is wrong is for the pivot table name for the many to many relation between my Customer Model and Chatter Model. Filament tries to do the inner join with the table named chatter_customer while my table is named chatting_instances. I can't figure out how to change this behaviour
nanopanda
nanopanda6mo ago
@taz have you tried passing in the table name to your belongsToMany() call? Something like: return $this->belongsToMany( Customer::class, 'chatting_instances' )->...
taz
tazOP6mo ago
@nanopanda thanks a lot for your help, that was it. It's not even related to Filament, it's Eloquent that determines the pivot table name for the DB request and it's clearly written in the Laravel Documentation : To determine the table name of the relationship's intermediate table, Eloquent will join the two related model names in alphabetical order. However, you are free to override this convention. You may do so by passing a second argument to the belongsToMany method
Want results from more Discord servers?
Add your server