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

SYED_IRTAZA_HASNAIN's avatar

Laravel Filament, search is not working on accessors columns department_name

Anyone implemented , Filament search We cant apply search on accessor column, I got error SQLSTATE[42S22]: Column not found: 1054 Unknown column 'department_name' in 'where clause'

JobApplication.php (Model) public function job() { return $this->belongsTo(Jobs::class); }

public function getDepartmentNameAttribute()
{
    return optional($this->job()->withTrashed()->first())->department->name ?? null;
}

Filament JobApplication Resource Tables\Columns\TextColumn::make('department_name') ->label('Department Name') ->searchable(isIndividual: false, isGlobal: true),

0 likes
9 replies
Tray2's avatar

Make sure that your table has a column named department_name and if it doesn't make sure that you enter the correct column name in your text column definition.

SYED_IRTAZA_HASNAIN's avatar

@Tray2 I am using accessors to fetch the record , the column department_name is not present in table, as iI am using relation to fetch the record , Filament need to filter the record on the relational data

Tray2's avatar

@SYED_IRTAZA_HASNAIN Then you need to change this line

Filament JobApplication Resource Tables\Columns\TextColumn::make('department_name')

Because it's looking for the department name column

SYED_IRTAZA_HASNAIN's avatar

@Tray2 Basically department_name is a virtual based column , used to fetch the record , I want to apply search on this virtual column , is there anyway , without inserting this column into the job_application table , by virtual column the search functionality works , if I make another column in database of department_name its not good approach as table alos have foreign key job_id.

public function getDepartmentNameAttribute() { return optional($this->job()->withTrashed()->first())->department->name ?? null; }

Tray2's avatar

@SYED_IRTAZA_HASNAIN have you considered using a virtual column in your table?

I use that in one of my projects.

 Schema::create('authors', function (Blueprint $table) {
            $table->id();
            $table->string('first_name');
            $table->string('last_name');
            $table->string('name')
                ->virtualAs("CONCAT(last_name, ', ',  first_name)")
                ->unique();
            $table->timestamps();
        });
SYED_IRTAZA_HASNAIN's avatar

@Tray2 No I didnt added virtual column in schema, if I need to add a column in database, then I would add it and set mutators and observers for the specific column ,but I was looking for how filament perform search on columns that are fetching record using eloquent relations. E.g I'm in Job_application table in this tabe I have job_id foreign key, in Job table there is department_id Now using accesor i fetch the department 'name' in job_application model now on department_name i want to apply search , but that column isnt physically present. So how filament will going to perform search on this specific relation.

I've implemented one solution by adding column in database of department_name i can perform search but , its not good idea to insert record on multiple tables

Tray2's avatar

@SYED_IRTAZA_HASNAIN True, it is not optimal, but you can always use a view instead of a table, then you solve that issue.

When I was playing around a little with filament, I did this to fetch the name of the relation item, and it seems similar to what you are trying to do.

Select::make('format_id')
                            ->label('Format')
                            ->required()
                            ->searchable()
                            ->options(Format::where('type', $book)->pluck('name', 'id')),
SYED_IRTAZA_HASNAIN's avatar

@Tray2 Heres the correct solution to apply search on accessors "department_name" without making changes in database

Tables\Columns\TextColumn::make('department_name') ->label('Department Name') ->searchable(query: function (Builder $query, string $search): Builder { return $query->whereHas('job', function (Builder $jobQuery) use ($search) { $jobQuery->whereHas('department', function ($departmentQuery) use ($search) { $departmentQuery->where('name', 'like', "%{$search}%"); }); }); }),

2 likes

Please or to participate in this conversation.