How to create a multiple primary column key in an eloquent

I store managers, Course_Type and Course Years in the database.

I will call my courses according to increasing values, such as

    Course_type    course_order      course_year

    Course_type[0]      1              2015 
    Course_type[0]      2              2015
    Course_type[0]      3              2015
    Course_type[0]      4              2015
    Course_type[0]      1              2016
    Course_type[1]      1              2015
    Course_type[2]      1              2015
    Course_type[2]      2              2015

So I am creating a database like

Schema::create('courses', function (Blueprint $table) {
            $table->increments('id');
            $table->enum('course_type', [
                'Yos',
                'TomerABC',
                'TomerA1A2',
                'TomerB1B2',
                'TomerC1C2',
                'TomerAB',
                'TomerBC',
                'SatMatGeo',
                'SatCriRea',
                'SatCriReaMat',

            ]);

            $table->integer('added_by');
            $table->integer('start');
            $table->integer('year_code');
            $table->integer('end');
            $table->integer('course_order');
            $table->unique( array('added_by','year_code','course_order') );

and this is my controller on createprocess

$courseorder = \App\Courses::where('added_by', \Auth::id())
            ->where('year_code', $input['year_code'])->max('course_order');

$course->course_order = $courseorder + 1;

but since I'm looking for max ('course_order') in the database, if I delete, say, the order of course 2. The next added course will be 4 instead of 3.

enter image description here

So my question is, how can I check for missing values ​​between the course_order to fill a position after it is deleted?

PS mysql solution will be faster and better than php, I think.

+4
source share
1

. , DeletedCourses. DeletedRecord , DeletedRecords .

+1

Source: https://habr.com/ru/post/1624433/


All Articles