Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ritechoice23's avatar

Query is returning null in filament but works outside filament

please help me with this situation, if I use this relationship outside of filament it works well but inside filament, it is changing the $this->programme_id to null.

public function programmeCourse(): BelongsTo { return $this->belongsTo(ProgrammeCourse::class, 'course_id', 'course_id') ->where('programme_id', $this->programme_id); }

here is the resulting query inside filament select * from programme_courses where programme_id is null and programme_courses.course_id in (3, 4, 5, 8, 10, 14, 19, 23, 25, 35)

0 likes
10 replies
tykus's avatar

You cannot use $this->programme_id in the context of eager-loading relations; there is no hydrated Model instance to work with.

Can you share how you are using the relationship outside Filament, so we can understand how it is working?

ritechoice23's avatar

@tykus outside filament it is working this way $registration->programmeCourses, but in filament it does not work.

tykus's avatar

That's because you are working with an instance of the Model, so $this->programme_id has a value. Whereas, in Filament, it is likely being eager-loaded (and therefore an instance of Eloquent's Builder).

Can you show how the Relation is being used in the Filament Resource class?

krisi_gjika's avatar

try maybe like:

->whereColumn(ProgrammeCourse::getTable().'.programme_id', static::getTable().'.programme_id')
jaseofspades88's avatar

It isn't difficult. Show us what you're doing both inside and outside Filament and maybe we can help. We shouldn't have to beg you to give us more information to help you fix your problem.

jaseofspades88's avatar

Use markdown for your code snippets using backticks, please and thank you

ritechoice23's avatar

@jaseofspades88 This is what I want to be able to do in filament, using the relationship above

TextColumn::make('programmeCourse.units')

I only tried it out in the model to trace if things is working using the code below

   static::retrieved(function ($model) {
            dd($model, $model->programmeCourse);
        });
tykus's avatar

@horlatech like I explained already; you are, in the relationship definition, attempting to use state that is not available to the Builder in the context of eager-loaded relationship queries.

Consider amending the relationship, or using a JOIN instead.

Please or to participate in this conversation.