除了模型中的分配方法之外,您還可以透過定義新方法來進行檢查:
class Assignment extends Model
{
public function assignments()
{
return $this->hasMany(<YOUR_ASSIGNMENTS_TABLE_NAME>::class, 'id_assignment', 'id_id');
}
// new method
public function checkStudentAssignmentIsNull()
{
return $this->hasMany(<YOUR_ASSIGNMENTS_TABLE_NAME>::class, 'id_assignment', 'id_id')
->where('<YOUR_ASSIGNMENTS_TABLE_NAME>.id_student', Auth::user()->id)
->first() === null;
}
}
使用時,可以使用 !$data->checkStudentAssignmentIsNull()
而不是 !empty($data->assignments->id_student)
。
我現(xiàn)在可以更清楚地看到您的表格:
// new method
public function checkStudentAssignmentIsNull()
{
return $this->hasMany(AssignmentDetail::class, 'id_assignment', 'id_id')
->where('assignment_details.id_student', Auth::user()->id)
->first() === null;
}
點贊 +0
P粉373990857