F
FilamentCalden

Column relationships problem

Hey ! In a relation manager resource, i have this code :
public function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('enseignants')
->badge(),
])
public function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('enseignants')
->badge(),
])
with just "Tables\Columns\TextColumn::make('enseignants')", i have my badges filled with json, like "{"id":15,"civilite_id":2,"prenom":"Audrey","nom" ...." . Usually i have to add the field, like "Tables\Columns\TextColumn::make('enseignants.prenom')" to display only "prenom" in the badge. but when i add it, i have a "Call to a member function enseignants() on null" error. The models is :
class MatiereSession extends Pivot
{
use HasFactory;

protected $table = 'matiere_session';

public function matiere(): BelongsTo
{
return $this->belongsTo(Matiere::class);
}

public function session(): BelongsTo
{
return $this->belongsTo(Session::class);
}

public function enseignants()
{
return $this->matiere->enseignants()->wherePivot('session_id', $this->session->id);

}
class MatiereSession extends Pivot
{
use HasFactory;

protected $table = 'matiere_session';

public function matiere(): BelongsTo
{
return $this->belongsTo(Matiere::class);
}

public function session(): BelongsTo
{
return $this->belongsTo(Session::class);
}

public function enseignants()
{
return $this->matiere->enseignants()->wherePivot('session_id', $this->session->id);

}
` I dont know what to do to make it work :/
LZ
Lara Zeus13d ago
looks like one of the record dont have data for matiere!
C
Calden12d ago
Ok, i have delete records, i now have just one "MatiereSession record, when i add a Tables\Columns\TextColumn::make('matiere.nom') i have the name of the "matiere" so there is datas for matiere ? When i have only "Tables\Columns\TextColumn::make('enseignants')", i have :
C
Calden12d ago
No description
C
Calden12d ago
when i have "Tables\Columns\TextColumn::make('enseignants.prenom')", i get the error :
C
Calden12d ago
No description
C
Calden12d ago
I'm on the "matieresSession" relationmanager of the "MatiereResource" where "Matiere" model is :
class Matiere extends Model
{
use HasFactory;

public function enseignants(): BelongsToMany
{
return $this->belongsToMany(Enseignant::class, 'enseignant_matiere')->orderBy('prenom_nom');
}

public function sessions(): BelongsToMany
{
return $this->belongsToMany(Session::class);
}

public function matieresSession(): HasMany
{
return $this->hasMany(MatiereSession::class);
}
class Matiere extends Model
{
use HasFactory;

public function enseignants(): BelongsToMany
{
return $this->belongsToMany(Enseignant::class, 'enseignant_matiere')->orderBy('prenom_nom');
}

public function sessions(): BelongsToMany
{
return $this->belongsToMany(Session::class);
}

public function matieresSession(): HasMany
{
return $this->hasMany(MatiereSession::class);
}
i got the correct result when i use the following code :
Tables\Columns\TextColumn::make('enseignants')
->getStateUsing(function (Model $record) {
$name_list = array();
$list = $record->matiere->enseignants()->wherePivot('session_id', $record->session_id)->get()->toArray();
foreach ($list as $subarray) {
if (isset($subarray["prenom_nom"])) {
$name_list[] = $subarray["prenom_nom"];
}
}
return $name_list;
})
->label('Enseignants')
->badge()
->placeholder('Aucun')
->searchable(),
])
Tables\Columns\TextColumn::make('enseignants')
->getStateUsing(function (Model $record) {
$name_list = array();
$list = $record->matiere->enseignants()->wherePivot('session_id', $record->session_id)->get()->toArray();
foreach ($list as $subarray) {
if (isset($subarray["prenom_nom"])) {
$name_list[] = $subarray["prenom_nom"];
}
}
return $name_list;
})
->label('Enseignants')
->badge()
->placeholder('Aucun')
->searchable(),
])
But why i cant use this ?
Tables\Columns\TextColumn::make('enseignants.prenom_nom')
Tables\Columns\TextColumn::make('enseignants.prenom_nom')
Want results from more Discord servers?
Add your server
More Posts
custom themeHello, when I create a custom theme (php artisan make:filament-theme) and configure my vite.js fToggle-columnIs there any way to make toggle-column from scratch on a custom page ?$getAction() and visibilityWhen using ->registerActions() on a Form or Infolist component, it seems that visible() and hidden()Modal action or table inside formMy use case is a CRM application where each resource only has an edit screen. A list of related resoCustomizing Data Insertion with Filament's Import ActionHello, Regarding the import: https://filamentphp.com/docs/3.x/actions/prebuilt-actions/import Is iCount SaleItems sold by Product IDHello, I have a Sales Resource with (hasMany) SaleItems Resource which selects a product to sell froMissing required parameter for View routeI have just found out the wonder of Repeaters, and after adding one to a form, the first time I submDownloadable() and Openable() gives me 404 errorhello! I am currently creating our system and the downloadable() and openable() syntax in my filamenhow to add external url in navigationi want to add external url in my navigation sidebar. but i find a way for a specific resource. whatcustom themeHas anyone used Multi-Tenancy with diferent DB Sqlite???Livewire View Not foundI have a brand new install of Laravel 11, Filament 3 and Livewire 3. I have been configuring the adBumped from 3 to 3.2 and getting Leage\Csv\Writer not found```/vendor/filament/actions/src/Exports/Jobs/PrepareCsvExport.php:53 Leage\Csv\Writer not found ``` Using TailwindHi I have a project where I'm using Tailwind. I have a related resouce, which I want to highlught Deferring loadingWhat does it actually do for tables?Column not found with table filter and joinsHello everyone, I am working on my first project with filament and I have hit a brick wall. I'm trTrying to find a way to show the infolist in the EditResource pageis it possible? so far i've just managed to make it appear in the view page, but id like it to appeahow to update another model instead of updating current resource?hello i want to insert data into another model first when user edit a form. what should i do?Laravel-Filament Qr generateHello, I hope you have a good day. I want to make a QR converter based on Laravel-filament. I want a