F
Filament8mo ago
Calden

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 :/
6 Replies
Lara Zeus
Lara Zeus8mo ago
looks like one of the record dont have data for matiere!
Calden
CaldenOP8mo 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 :
Calden
CaldenOP8mo ago
No description
Calden
CaldenOP8mo ago
when i have "Tables\Columns\TextColumn::make('enseignants.prenom')", i get the error :
Calden
CaldenOP8mo ago
No description
Calden
CaldenOP8mo 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