Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::getOwnerKeyName()

Hi there! Created a simple BelgonsToMany relationship like in Laravel Documentation(https://laravel.com/docs/10.x/eloquent-relationships#many-to-many-defining-the-inverse-of-the-relationship) and I've got the following error:
Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::getOwnerKeyName()
Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::getOwnerKeyName()
I've used the example from the FIlament documentation for creating a Select:
Forms\Components\Select::make('projects')
->relationship('projects', 'name'),
Forms\Components\Select::make('projects')
->relationship('projects', 'name'),
https://flareapp.io/share/Lm8MOMpP Thank you!
Flare
Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::getOwnerKeyName() - The error occurred at http://disponibilitati.test/admin/apartments/create
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
12 Replies
Jordy
Jordy2y ago
guess you called the value before "getOwnerKeyName()" as a method, not as a value? I am guessing it was a user.. call user->getOwnerKeyName() calling it as a method will return the relation, not the instance
Alex Manase
Alex ManaseOP2y ago
I did not called it. The only place where I used the relationship is in this piece of code:
Forms\Components\Select::make('projects')
->relationship('projects', 'name'),
Forms\Components\Select::make('projects')
->relationship('projects', 'name'),
Jordy
Jordy2y ago
have you pinpointed what line throws the error?
Alex Manase
Alex ManaseOP2y ago
GitHub
filament/packages/forms/src/Components/Select.php at 633ea6f739ced1...
Admin panel, form builder and table builder for Laravel. Built with the TALL stack. Designed for humans. - filament/packages/forms/src/Components/Select.php at 633ea6f739ced12b4dd4d9403ce5bc66efa1e...
Alex Manase
Alex ManaseOP2y ago
Thsi one
Jordy
Jordy2y ago
and your relations are? so I can try to replicate it
Alex Manase
Alex ManaseOP2y ago
One moment
class Apartment extends Model
{
//
public function projects(): BelongsToMany
{
return $this->belongsToMany(Project::class);
}
}
class Apartment extends Model
{
//
public function projects(): BelongsToMany
{
return $this->belongsToMany(Project::class);
}
}
class Project extends Model
{
use HasFactory;

public function apartments(): BelongsToMany
{
return $this->belongsToMany(Apartment::class);
}
}
class Project extends Model
{
use HasFactory;

public function apartments(): BelongsToMany
{
return $this->belongsToMany(Apartment::class);
}
}
class ApartmentProject extends Pivot
{
}
class ApartmentProject extends Pivot
{
}
Schema::create('apartment_project', function (Blueprint $table) {
$table->id();

$table->foreignId('apartment_id');
$table->foreignId('project_id');

$table->timestamps();
});
Schema::create('apartment_project', function (Blueprint $table) {
$table->id();

$table->foreignId('apartment_id');
$table->foreignId('project_id');

$table->timestamps();
});
Jordy
Jordy2y ago
getting the same error now @alexmanase I do not know the fix, seems like a bug.. but you could use this workaround:
Select::make('apartments')
->options($this->project->apartments->pluck('name', 'id')->toArray())
Select::make('apartments')
->options($this->project->apartments->pluck('name', 'id')->toArray())
or the other way around with projects, does not make a difference
Alex Manase
Alex ManaseOP2y ago
Thanks! I know that, but I need to create a new relationship model from the select.
->createOptionForm([
Forms\Components\TextInput::make('name')
->required(),
]),
->createOptionForm([
Forms\Components\TextInput::make('name')
->required(),
]),
Like this. I'll make an issue to the Filament repo Thanks for your help.
awcodes
awcodes2y ago
I don’t think you can use a many to many on a select without making it multiple. Might also need 'withPivot' on your relationships
Alex Manase
Alex ManaseOP2y ago
GitHub
Call to undefined method Illuminate\Database\Eloquent\Relations\Bel...
Package filament/filament Package Version ^2.0 Laravel Version ^10.10 Livewire Version ^2.12 PHP Version 8.2.7 Problem description When I try to use a select for a resource using BelongsToMany rela...
Alex Manase
Alex ManaseOP2y ago
Thanks!
Want results from more Discord servers?
Add your server