mercredi 20 avril 2022

Laravel increment with custom starting point

I am trying to allot roll numbers to students. there are a lot of students so when I try to do so using eloquent (one by one) server timeout occurs. any idea on how to do so in single update query.

Here is the basic code:

$roll_number = ExamFormSetting::INITIAL_ROLL_NUMBER;

$max_value = StudentExamForm::ofExamYear(request('exam_year_id'))
                                ->max('roll_number');

if ( $max_value > $roll_number ) {

    $roll_number = (integer)$max_value + 1;

}
//after this I query required info and assign roll number one by one which is not optimal solution. 

Here I am getting the current max roll number from the table. each roll number should be unique every year.

If there is no record i.e. max value is empty I want to start from a given number.

Now I want to assign roll numbers using a single update query.

Here is the code to update the roll number.

StudentExamForm::select('student_exam_forms.*')
                   ->ofDegreePart(request('degree_part_id'))
                   ->ofExamType(request('exam_type_id'))
                   ->ofExamYear(request('exam_year_id'))
                   ->join('students', 'students.id', '=', 'student_exam_forms.student_id')
                   ->orderByDesc('theory_exam_center_id')
                   ->orderBy('students.name')
                   ->increment('roll_number', 1);

but the problem is all the selected rows have null in roll number so the increment is not working. Any option to allot roll number to the first row and later increment remaining rows.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/JQOtk64
via IFTTT

Aucun commentaire:

Enregistrer un commentaire