Can Query Model Pivot Custom Table without field id?

I'm trying to create a Custom Table with the Pivot Model Enrollment, but I encountered an error. I don't have the id field in the enrollments pivot table. How can I modify the query without using ORDER BY id? Should I add the id field to the pivot table? - Error: SQLSTATE[42703]: Undefined column: 7 ERROR: column enrollments.id does not exist LINE 1: select * from "enrollments" order by "enrollments"."id" asc ... ^
SELECT * FROM "enrollments" ORDER BY "enrollments"."id" ASC limit 10 OFFSET 0
SELECT * FROM "enrollments" ORDER BY "enrollments"."id" ASC limit 10 OFFSET 0
1 Reply
Kaesa Lyrih
Kaesa LyrihOP7mo ago
- Table enrollments
Schema::create('enrollments', function (Blueprint $table) {
$table->unsignedBiginteger('classroom_id')->unsigned();
$table->unsignedBiginteger('student_id')->unsigned();
$table->foreign('classroom_id')->on('classrooms')->references('id')->onDelete('cascade');
$table->foreign('student_id')->on('students')->references('id')->onDelete('cascade');
$table->primary(['classroom_id', 'student_id']);
$table->timestamps();
});
Schema::create('enrollments', function (Blueprint $table) {
$table->unsignedBiginteger('classroom_id')->unsigned();
$table->unsignedBiginteger('student_id')->unsigned();
$table->foreign('classroom_id')->on('classrooms')->references('id')->onDelete('cascade');
$table->foreign('student_id')->on('students')->references('id')->onDelete('cascade');
$table->primary(['classroom_id', 'student_id']);
$table->timestamps();
});
- Pivot Model Enrollment
protected $table = 'enrollments';
protected $foreignKey = 'classroom_id';
protected $relatedKey = 'student_id';
public $incrementing = false;

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

public function student(): BelongsTo
{
return $this->belongsTo(Student::class);
}
protected $table = 'enrollments';
protected $foreignKey = 'classroom_id';
protected $relatedKey = 'student_id';
public $incrementing = false;

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

public function student(): BelongsTo
{
return $this->belongsTo(Student::class);
}
- Custom Table Livewire ListStudentEnrollments
<?php

// ...

class ListStudentEnrollments extends Component implements HasForms, HasTable
{
use InteractsWithForms;
use InteractsWithTable;

public function table(Table $table): Table
{
return $table
->query(Enrollment::query())
->columns([
Tables\Columns\TextColumn::make('classroom.name')
->sortable(),
Tables\Columns\TextColumn::make('classroom.end_date')
->dateTime()
->sortable(),
])
->filters([
//
])
->actions([
//
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
//
]),
]);
}

public function render(): View
{
return view('livewire.guardian.list-student-enrollments');
}
}
<?php

// ...

class ListStudentEnrollments extends Component implements HasForms, HasTable
{
use InteractsWithForms;
use InteractsWithTable;

public function table(Table $table): Table
{
return $table
->query(Enrollment::query())
->columns([
Tables\Columns\TextColumn::make('classroom.name')
->sortable(),
Tables\Columns\TextColumn::make('classroom.end_date')
->dateTime()
->sortable(),
])
->filters([
//
])
->actions([
//
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
//
]),
]);
}

public function render(): View
{
return view('livewire.guardian.list-student-enrollments');
}
}
Want results from more Discord servers?
Add your server