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');
}
}
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);
}
}
class PendingEmailStudentPortal extends Model
{
use HasFactory;

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

public function students(): HasMany
{
return $this->hasMany(Student::class);
}
}
No description
No description
Solution:
Thanks, I changed the relationship to BelongsTo then added ->sortable() to the student_no column.
Jump to solution
4 Replies
Vhelkhana
Vhelkhana2mo ago
Student model:
class Student extends Model
{
use HasFactory;

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

public function degreeProgram(): BelongsTo
{
return $this->belongsTo(DegreeProgram::class);
}

public function birthplaceCity(): BelongsTo
{
return $this->belongsTo(City::class, 'birthplace_city_id');
}

public function city(): BelongsTo
{
return $this->belongsTo(City::class);
}

public function biologicalSex(): BelongsTo
{
return $this->belongsTo(BiologicalSex::class);
}

public function civilStatus(): BelongsTo
{
return $this->belongsTo(CivilStatus::class);
}

public function registrationStatus(): BelongsTo
{
return $this->belongsTo(RegistrationStatus::class);
}

public function studentFamily(): HasOne
{
return $this->hasOne(StudentFamily::class);
}

public function assignedClasses(): HasMany
{
return $this->hasMany(AssignedClass::class);
}

public function studentRecords(): HasMany
{
return $this->hasMany(StudentRecord::class);
}

public function studentViolations(): HasMany
{
return $this->hasMany(StudentViolation::class);
}

public function studentRequests(): HasMany
{
return $this->hasMany(StudentRequest::class);
}

public function academicYear(): BelongsTo
{
return $this->belongsTo(AcademicYear::class);
}
}
class Student extends Model
{
use HasFactory;

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

public function degreeProgram(): BelongsTo
{
return $this->belongsTo(DegreeProgram::class);
}

public function birthplaceCity(): BelongsTo
{
return $this->belongsTo(City::class, 'birthplace_city_id');
}

public function city(): BelongsTo
{
return $this->belongsTo(City::class);
}

public function biologicalSex(): BelongsTo
{
return $this->belongsTo(BiologicalSex::class);
}

public function civilStatus(): BelongsTo
{
return $this->belongsTo(CivilStatus::class);
}

public function registrationStatus(): BelongsTo
{
return $this->belongsTo(RegistrationStatus::class);
}

public function studentFamily(): HasOne
{
return $this->hasOne(StudentFamily::class);
}

public function assignedClasses(): HasMany
{
return $this->hasMany(AssignedClass::class);
}

public function studentRecords(): HasMany
{
return $this->hasMany(StudentRecord::class);
}

public function studentViolations(): HasMany
{
return $this->hasMany(StudentViolation::class);
}

public function studentRequests(): HasMany
{
return $this->hasMany(StudentRequest::class);
}

public function academicYear(): BelongsTo
{
return $this->belongsTo(AcademicYear::class);
}
}
Vp
Vp2mo ago
I am not sure but I think because of hasMany() relationship. If you query then you'll get array in relationship, not object. but not sure tho
Swan_Yee
Swan_Yee2mo ago
PendingEmailStudentPortal to student Rs should be like this
class PendingEmailStudentPortal extends Model
{
use HasFactory;

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

public function student(): BelongTo
{
return $this->belongTo(Student::class);
}
}
class PendingEmailStudentPortal extends Model
{
use HasFactory;

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

public function student(): BelongTo
{
return $this->belongTo(Student::class);
}
}
I think u need to learn about laravel Model relationship
Solution
Vhelkhana
Vhelkhana2mo ago
Thanks, I changed the relationship to BelongsTo then added ->sortable() to the student_no column.
Want results from more Discord servers?
Add your server
More Posts