Np
Managing relationship
i want have pivot table with model_id , role_id, branch_id , model_type
while creating i want to attach all above fields . Please find the relationships its not working
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('company_id')
->relationship('company', 'name')
->required(),
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\TextInput::make('email')
->email()
->required()
->maxLength(255),
Forms\Components\DateTimePicker::make('email_verified_at'),
Forms\Components\TextInput::make('password')
->password()
->required()
->maxLength(255),
Forms\Components\Select::make('Branches')
->relationship('branches', 'name')
->multiple()
->required(),
Forms\Components\Select::make('roles')
->relationship('roles', 'name')
->multiple()
->required(),
Forms\Components\Select::make('rolesBranch')
->relationship('rolesBranch','name')
->multiple()
->required(),
]);
}
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class, 'model_has_roles', 'model_id', 'role_id')->withPivot('branch_id', 'model_type');
}
public function rolesBranch(): BelongsToMany
{
return $this->belongsToMany(Branch::class, 'model_has_roles', 'model_id', 'branch_id')->withPivot('role_id', 'model_type');
}
2 replies