can't assign role to customer

i was trying to assign when customer is created i created a user for the customer in aftercreate method then assign customer role but am getting this error and here is my code resource
->schema([
Forms\Components\DatePicker::make('date')
->firstDayOfWeek(6)
->maxDate(now())
->closeOnDateSelection()
->default(Carbon::now())
->native(false)->required(),
Forms\Components\Radio::make('type')
->options([
'individual' => 'Individual',
'business' => 'Business',
])->required()->default('individual')->columns(4),
Forms\Components\TextInput::make('name')->required(),
Forms\Components\TextInput::make('mobile')->required()->unique(ignoreRecord: true),
Forms\Components\TextInput::make('email')->required()->email()->unique(ignoreRecord: true),

Forms\Components\Radio::make('access')
->required()
->label('Portal Access')
->live()
->options([
true => 'Allow',
false => 'Not Allow',
])->default(false)->columns(4),

Forms\Components\Select::make('roles')
->live()
->hidden(fn(\Filament\Forms\Get $get) => $get('access') == false)
->required(function (\Filament\Forms\Get $get) {
return $get('access') == true;
})
->relationship('roles', 'name')
->multiple()
->preload()
->searchable(),

]);
->schema([
Forms\Components\DatePicker::make('date')
->firstDayOfWeek(6)
->maxDate(now())
->closeOnDateSelection()
->default(Carbon::now())
->native(false)->required(),
Forms\Components\Radio::make('type')
->options([
'individual' => 'Individual',
'business' => 'Business',
])->required()->default('individual')->columns(4),
Forms\Components\TextInput::make('name')->required(),
Forms\Components\TextInput::make('mobile')->required()->unique(ignoreRecord: true),
Forms\Components\TextInput::make('email')->required()->email()->unique(ignoreRecord: true),

Forms\Components\Radio::make('access')
->required()
->label('Portal Access')
->live()
->options([
true => 'Allow',
false => 'Not Allow',
])->default(false)->columns(4),

Forms\Components\Select::make('roles')
->live()
->hidden(fn(\Filament\Forms\Get $get) => $get('access') == false)
->required(function (\Filament\Forms\Get $get) {
return $get('access') == true;
})
->relationship('roles', 'name')
->multiple()
->preload()
->searchable(),

]);
i think the relationship is causing the error any idea plz
No description
8 Replies
Povilas K
Povilas K3mo ago
@Adnan Yalahow You didn't post the code from your afterCreate() so it's hard to say. But the error points at the fact that USER is not passed correctly to the ->assignRole() method of Spatie package
Adnan Yalahow
Adnan Yalahow3mo ago
here is my aftercreate function
protected function afterCreate(): void
{
dd('Am here');
// create user record if access is allowed
if ($this->record['access']) {
$user = new User([
'name' => $this->record['name'],
'phone' => $this->record['mobile'],
'email' => $this->record['email'],
'password' => Hash::make('12345678'),
'password_updated' => false,
]);
$user->save();
$roleNames = [];
foreach ($this->data['roles'] as $role) {
$finalRole = Role::find($role);
$roleNames[] = $finalRole->name;
}
$user->assignRole($roleNames);

$this->record['user_id'] = $user->id;
$this->record->save();
}

}
protected function afterCreate(): void
{
dd('Am here');
// create user record if access is allowed
if ($this->record['access']) {
$user = new User([
'name' => $this->record['name'],
'phone' => $this->record['mobile'],
'email' => $this->record['email'],
'password' => Hash::make('12345678'),
'password_updated' => false,
]);
$user->save();
$roleNames = [];
foreach ($this->data['roles'] as $role) {
$finalRole = Role::find($role);
$roleNames[] = $finalRole->name;
}
$user->assignRole($roleNames);

$this->record['user_id'] = $user->id;
$this->record->save();
}

}
the reason i did not post is because its not reached i cant even see ""Am here""
Povilas K
Povilas K3mo ago
So where is that screenshot error comes from if your assignRole() code isn't even called?
Adnan Yalahow
Adnan Yalahow3mo ago
am thinking this part of the form is causing the error that relationship line
Forms\Components\Select::make('roles')
->live()
->hidden(fn(\Filament\Forms\Get $get) => $get('access') == false)
->required(function (\Filament\Forms\Get $get) {
return $get('access') == true;
})
->relationship('roles', 'name')
->multiple()
->preload()
->searchable(),
Forms\Components\Select::make('roles')
->live()
->hidden(fn(\Filament\Forms\Get $get) => $get('access') == false)
->required(function (\Filament\Forms\Get $get) {
return $get('access') == true;
})
->relationship('roles', 'name')
->multiple()
->preload()
->searchable(),
and this is my customer model
class Customer extends Model
{
use HasFactory;
use HasRoles;

protected $table = 'customers';
protected $fillable = ['date', 'type', 'name', 'mobile', 'email', 'area', 'user_id','added_by', 'access'];
}
class Customer extends Model
{
use HasFactory;
use HasRoles;

protected $table = 'customers';
protected $fillable = ['date', 'type', 'name', 'mobile', 'email', 'area', 'user_id','added_by', 'access'];
}
Povilas K
Povilas K3mo ago
In this code, I don't see anything that would automatically save any roles to "model_has_roles" table. That DB table comes from the package, and from what I remember it's called only in methods like ->assignRole() which I don't see in your latest code. Sorry, I guess it's not solvable on Discord without real debugging and putting dd() everywhere until you find where the error comes from 🙂
Adnan Yalahow
Adnan Yalahow3mo ago
i have been really trying hard on this. But would you suggest anything from your experience, I am trying to create a customer and i want to give a role as i create that customer and after i will use that for the customer to view his own data i really appriciate you assistance
Povilas K
Povilas K3mo ago
Adnan Yalahow
Adnan Yalahow3mo ago
Thank you i will check out
Want results from more Discord servers?
Add your server
More Posts