Column relationships dot syntax not working

Why is students.[column] not working? I am getting this error: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'students.student_no' in 'order clause'
SELECT * FROM
pending_email_student_portals
ORDER BY
students
.
student_no
ASC limit 10 OFFSET 0"

ListPendingEmailStudentPortal.php:
    public function table(Table $table): Table
    {
        return $table
            ->query(PendingEmailStudentPortal::query())
            ->columns([
                TextColumn::make('students.student_no')
                    ->label('Student Number'),
                TextColumn::make('students.last_name')
                    ->label('Last Name'),
                TextColumn::make('students.first_name')
                    ->label('First Name'),
                TextColumn::make('students.middle_name')
                    ->label('Middle Name'),
                TextColumn::make('students.entry_date')
                    ->label('Entry Date'),
                TextColumn::make('Status')
                    ->default('Pending')
            ])
            ->defaultSort('students.student_no', 'asc');
    }
}


Students table:
[first pic]

PendingEmailStudentPortals table:
[second pic]

PendingEmailStudentPortal model:
class PendingEmailStudentPortal extends Model
{
    use HasFactory;

    protected $guarded = [
        'id',
        'created_at',
        'updated_at',
    ];

    public function students(): HasMany
    {
        return $this->hasMany(Student::class);
    }
}
image.png
image.png
Solution
Thanks, I changed the relationship to BelongsTo then added ->sortable() to the student_no column.
Was this page helpful?