Pivot table with extra json column

Is there any possibilty to save data (from a [TagsInput]-field) to an extra json column in a pivot table. I get an ErrorException: "Array to string conversion". Do I have to apply a cast somewhere?
9 Replies
Patrick Boivin
Can you show the code you've been trying?
MeniV
MeniVOP2y ago
PersoonRelationManager.php:
->schema([
Forms\Components\TextInput::make('voornaam')->label('Voornaam'),
Forms\Components\TextInput::make('achternaam')->label('Achternaam'),
Forms\Components\TextInput::make('alias')->label('Alias'),
Forms\Components\TagsInput::make('rollen')->label('Rol'), // the json column
]),
->schema([
Forms\Components\TextInput::make('voornaam')->label('Voornaam'),
Forms\Components\TextInput::make('achternaam')->label('Achternaam'),
Forms\Components\TextInput::make('alias')->label('Alias'),
Forms\Components\TagsInput::make('rollen')->label('Rol'), // the json column
]),
Persoon.php:
class Persoon extends Model
{
protected $fillable = ['voornaam', 'achternaam','alias', 'rollen'];

protected $casts = ['rollen' => 'json',];

public function documents(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(\App\Models\Document::class, 'document_persoon', 'document_id', 'persoon_id')
->withPivot(['rollen'])->withTimestamps();
}
}
class Persoon extends Model
{
protected $fillable = ['voornaam', 'achternaam','alias', 'rollen'];

protected $casts = ['rollen' => 'json',];

public function documents(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(\App\Models\Document::class, 'document_persoon', 'document_id', 'persoon_id')
->withPivot(['rollen'])->withTimestamps();
}
}
Document.php:
protected $fillable = [
'name', 'descr', 'link', 'details',
];

protected $casts = [
'details' => 'json',
'rollen' => 'json',
];


public function persoons(): BelongsToMany
{
return $this->belongsToMany(Persoon::class, 'document_persoon', 'persoon_id', 'document_id')
->withPivot(['rollen'])->withTimestamps();
}


}
protected $fillable = [
'name', 'descr', 'link', 'details',
];

protected $casts = [
'details' => 'json',
'rollen' => 'json',
];


public function persoons(): BelongsToMany
{
return $this->belongsToMany(Persoon::class, 'document_persoon', 'persoon_id', 'document_id')
->withPivot(['rollen'])->withTimestamps();
}


}
Migrations:
Schema::create('document_persoon', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignIdFor(Persoon::class);
$table->foreignIdFor(Document::class);
$table->json('rollen')->nullable();
$table->timestamps();
Schema::create('document_persoon', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignIdFor(Persoon::class);
$table->foreignIdFor(Document::class);
$table->json('rollen')->nullable();
$table->timestamps();
Patrick Boivin
Have you tried casting to array instead of json?
MeniV
MeniVOP2y ago
yes, gives the same error maybe a bug? or not yet implemented?
Dennis Koch
Dennis Koch2y ago
The cast on person is useless. Your person model doesn’t have the data. You need a pivot model and set it there
MeniV
MeniVOP2y ago
Can't find any documentation on a pivotmodel... So I created a 'pivot model' named DocumentPersoon.php
class DocumentPersoon extends Model
{
protected $table = 'document_persoon';
protected $fillable = ['rollen'];
protected $casts = ['rollen' => 'json'];
}
class DocumentPersoon extends Model
{
protected $table = 'document_persoon';
protected $fillable = ['rollen'];
protected $casts = ['rollen' => 'json'];
}
How to use it in the relationmanager?
Dennis Koch
Dennis Koch2y ago
Pivot Model is Laravel related. Usage is in filament docs. Currently only on mobile so can’t search for you.
MeniV
MeniVOP2y 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.

Did you find this page helpful?