SelectFilter with relationship query
I have used SelectFilter and applying query like this but not getting selected value $acd_id inside where.
SelectFilter::make('academic_id')->label('Academic Year')->options(fn (Get $get): array => AcademicYear::all()->where('status' , 1)->pluck('month_label', 'id')->all()
)
->query(function (Builder $query, array $data): Builder {
return $query ->when( $data['value'], fn (Builder $query, $acd_id): Builder =>
$query->with('academic') ->whereHas('academic', function ( Builder $query, $acd_id ) { $query->where('academic_id','=', $acd_id ); }) ); })
return $query ->when( $data['value'], fn (Builder $query, $acd_id): Builder =>
$query->with('academic') ->whereHas('academic', function ( Builder $query, $acd_id ) { $query->where('academic_id','=', $acd_id ); }) ); })
7 Replies
Check the output of
$data
first.$data is coming like $data['value'] = '' or $data['value']='3'
So what happends when you dd the $acd_id in the closure.
I a getting error when I use $acd_id
Undefined variable $acd_id
Solution
try
->whereHas('academic', function(Builder $query) use ($acd_id) {
Yes it works Thanks