F
Filamentβ€’2mo ago
Matthew

JSON relationship column in table

I have a table with a JSON column, like this:
Schema::create('filament-latex', function (Blueprint $table) {
$table->id();
// ...
$table->json('collaborators_id')->nullable();
// ...
$table->timestamps();
});
Schema::create('filament-latex', function (Blueprint $table) {
$table->id();
// ...
$table->json('collaborators_id')->nullable();
// ...
$table->timestamps();
});
In my model, I cast it as an array:
protected $fillable = ['collaborators_id']; /// other columns omitted
protected $table = 'filament-latex';
protected $casts = [
'collaborators_id' => 'array',
];
protected $fillable = ['collaborators_id']; /// other columns omitted
protected $table = 'filament-latex';
protected $casts = [
'collaborators_id' => 'array',
];
And in my resource table,
TextColumn::make('collaborators')
->label('Collaborators')
->visible(! config('filament-latex.avatar-columns'))
->badge()
->color('info'),
TextColumn::make('collaborators')
->label('Collaborators')
->visible(! config('filament-latex.avatar-columns'))
->badge()
->color('info'),
Using tinker, the column is saved on my table like this:
collaborators_id: "["4","5"]",
collaborators_id: "["4","5"]",
How should I define my relationship such that the resource table shows the name of the users? I havent used relationships that much so Im a bit confused πŸ˜…
1 Reply
Matthew
MatthewOPβ€’2mo ago
I tried a belongsTo relationship, but that onlu showed the first user (user 4 as shown from tinker), and even then it was the whole record, and not the name. Doing TextColumn::make('collaborators.name') would just give me an error I dont need a pivot table for this, right? I should also mention Im building this for a plugin. I dont have direct access to the User class

Did you find this page helpful?