ToggleButtons in RelationManager Attach getting Error "Array to string conversion"

When trying to Attach new item with Pivot getting Array to String Error, with Edit working fine. Here are codes: Models
class Group extends Model
{
use HasFactory, SoftDeletes;

protected $fillable = [
'course_id',
'name',
'type',
];

protected $casts = [
'type' => GroupType::class,
'lecturer_type' => 'array',
];

public function course()
{
return $this->belongsTo(Course::class);
}

public function lecturers()
{
return $this->belongsToMany(Lecturer::class)
->withPivot(['lecturer_type']);
}
}
class Group extends Model
{
use HasFactory, SoftDeletes;

protected $fillable = [
'course_id',
'name',
'type',
];

protected $casts = [
'type' => GroupType::class,
'lecturer_type' => 'array',
];

public function course()
{
return $this->belongsTo(Course::class);
}

public function lecturers()
{
return $this->belongsToMany(Lecturer::class)
->withPivot(['lecturer_type']);
}
}
class Lecturer extends Model
{
use HasFactory, SoftDeletes, HasTranslations;

public $translatable = [
'first_name',
'last_name',
'father_name',
'position'
];

protected $fillable = [
'chair_id',
'first_name',
'last_name',
'father_name',
'position',
'photo',
];

public function chair()
{
return $this->belongsTo(Chair::class);
}

public function groups()
{
return $this->belongsToMany(Group::class)
->withPivot(['lecturer_type']);
}
}
class Lecturer extends Model
{
use HasFactory, SoftDeletes, HasTranslations;

public $translatable = [
'first_name',
'last_name',
'father_name',
'position'
];

protected $fillable = [
'chair_id',
'first_name',
'last_name',
'father_name',
'position',
'photo',
];

public function chair()
{
return $this->belongsTo(Chair::class);
}

public function groups()
{
return $this->belongsToMany(Group::class)
->withPivot(['lecturer_type']);
}
}
6 Replies
Arshavir
Arshavir2mo ago
->headerActions([
Tables\Actions\CreateAction::make(),
Tables\Actions\AttachAction::make()
->form(fn (AttachAction $action): array => [
$action->getRecordSelect(),
Forms\Components\ToggleButtons::make('lecturer_type')
->inline()
->multiple()
->options([
'practice' => 'Practice',
'lecture' => 'Lecture'
])
->colors([
'practice' => 'info',
'lecture' => 'warning',
])
->icons([
'practice' => 'heroicon-o-pencil',
'lecture' => 'heroicon-o-check-circle',
])
->required(),
])
->preloadRecordSelect()
->multiple(),
])
->headerActions([
Tables\Actions\CreateAction::make(),
Tables\Actions\AttachAction::make()
->form(fn (AttachAction $action): array => [
$action->getRecordSelect(),
Forms\Components\ToggleButtons::make('lecturer_type')
->inline()
->multiple()
->options([
'practice' => 'Practice',
'lecture' => 'Lecture'
])
->colors([
'practice' => 'info',
'lecture' => 'warning',
])
->icons([
'practice' => 'heroicon-o-pencil',
'lecture' => 'heroicon-o-check-circle',
])
->required(),
])
->preloadRecordSelect()
->multiple(),
])
New Group and Attach getting Error How to Solve this? Edit and Detach working fine Anyone please help to solve an issue
Arshavir
Arshavir2mo ago
GitHub
RelationManager with Pivot and Select Multi · filamentphp filament ...
Hi, it s possible to add multi select for pivot column in relation manager ? Example : But when i attach i have an error : "Array to string conversion" Thanks !
Arshavir
Arshavir2mo ago
But also there I didn't find solution Also no matter what i use ToggleButton, Checkbox List, or Select...
Dan Harrin
Dan Harrin2mo ago
Please do not email me questions in the future, if everyone did that I would have no time left. You have put a cast on each model, but the column is not on the model, its on the pivot table so you need a withCasts() on each relationship for these pivot attributes, or define a pivot model with $casts
Dan Harrin
Dan Harrin2mo ago
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.
Arshavir
Arshavir2mo ago
Thanks Dan, problem solved!