Filament Resource Create/Edit BadMethodCallException Call to undefined method HasMany::associate()

I have a User model as well as a Patch model, every patch HasMany users. What I am trying to achieve is: I want to add Users to Patch. Code:
class User extends Authenticatable
{
protected $fillable = [
'name',
'email',
'password',
'patch_no'
];
//....

//....
public function patch(): BelongsTo
{
return $this->belongsTo(Patch::class);
}
}

class Patch extends Model
{
use HasFactory;

protected $fillable = ['code'];

public function users(): HasMany
{
return $this->hasMany(User::class);
}

}
class User extends Authenticatable
{
protected $fillable = [
'name',
'email',
'password',
'patch_no'
];
//....

//....
public function patch(): BelongsTo
{
return $this->belongsTo(Patch::class);
}
}

class Patch extends Model
{
use HasFactory;

protected $fillable = ['code'];

public function users(): HasMany
{
return $this->hasMany(User::class);
}

}
User migration:
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->foreignId('patch_id')->nullable()->constrained('patches');
$table->rememberToken();
$table->timestamps();
});
}
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->foreignId('patch_id')->nullable()->constrained('patches');
$table->rememberToken();
$table->timestamps();
});
}
Patch migration
Schema::create('patches', function (Blueprint $table) {
$table->id();
$table->string('code');
$table->timestamps();
});
Schema::create('patches', function (Blueprint $table) {
$table->id();
$table->string('code');
$table->timestamps();
});
After this I created a filament-resource: PatchResource. Inside the form I did:
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('code')
->required()
->maxLength(255),
Forms\Components\Select::make('users')
->relationship('users', 'name')
->multiple()
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('code')
->required()
->maxLength(255),
Forms\Components\Select::make('users')
->relationship('users', 'name')
->multiple()
]);
}
What did I do wrong here?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server