unknown field on BelongsToMany relationship

In my UserResource class, I have a select:
Select::make('locations')
->multiple()
->relationship('locations', 'name')
->disabled(fn (Get $get) => $get('role_id') === 1)
->preload()
->searchable()
Select::make('locations')
->multiple()
->relationship('locations', 'name')
->disabled(fn (Get $get) => $get('role_id') === 1)
->preload()
->searchable()
When I attempt to save I get the error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'locations' in 'field list'
update
`users`
SET
`phone` =,
`is_active` = 1,
`locations` = [ "25" ],
...
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'locations' in 'field list'
update
`users`
SET
`phone` =,
`is_active` = 1,
`locations` = [ "25" ],
...
For some reason it's not automatically applying the pivot table records like the documentation indicates. I have relationships established on both models: User:
public function locations(): BelongsToMany
{
return $this->belongsToMany(Location::class);
}
public function locations(): BelongsToMany
{
return $this->belongsToMany(Location::class);
}
Locations:
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class);
}
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class);
}
Any idea what could be causing it? I do have a mutateFormDataBeforeSave method in my EditUser resource, but I'm not touching anything related to this relationship in that method.
Solution:
$get('role_id') === 1 will likely fail anyway as $get is a string so you need to either cast it with (int) or 1 = '1' I am wondering if because you are passing disabled/enabled one of the methods for the relationship is being skipped Can you move ->relationship() to below disabled?...
Jump to solution
14 Replies
toeknee
toeknee14mo ago
That is strange, can you confirm your version of filament please?
Jon Mason
Jon MasonOP14mo ago
v3.0.82
LeandroFerreira
LeandroFerreira14mo ago
Did you create as location_user ?
Jon Mason
Jon MasonOP14mo ago
yep, location_user is the table name.
toeknee
toeknee14mo ago
Are you casting locations on the user table?
Jon Mason
Jon MasonOP14mo ago
i'm not sure I know what you mean.. which tells me the answer is probably no Bump...Still hoping to get some help on this. I tried modifying the name of the Select to location_id and it just changes the error message. It's still looking for a location_id directly on the users table, instead of on the pivot.
toeknee
toeknee14mo ago
Does this happen when the field is disabled or not disabled
Jon Mason
Jon MasonOP14mo ago
interesting! if I comment out the ->disabled() line, it works fine..
toeknee
toeknee14mo ago
Is the field disabled when it fails to work
Jon Mason
Jon MasonOP14mo ago
no, it's not. That's what doesn't make sense. The record I'm working with doesn't have a role_id === 1, which is the disabled criteria, so it's enabled..
Solution
toeknee
toeknee14mo ago
$get('role_id') === 1 will likely fail anyway as $get is a string so you need to either cast it with (int) or 1 = '1' I am wondering if because you are passing disabled/enabled one of the methods for the relationship is being skipped Can you move ->relationship() to below disabled?
Jon Mason
Jon MasonOP14mo ago
ok that works...I didn't realize that order matters on these declarations.
Jon Mason
Jon MasonOP14mo ago
thank you!
toeknee
toeknee14mo ago
Yeah it can do depending on the complexity of the function. Glad to have helped!
Want results from more Discord servers?
Add your server