By GokiSoft.com| 19:55 05/12/2022|
Học Laravel

Tìm hiểu migration trong laravel - Lập trình laravel

Sử dụng migration trong laravel tạo 5 bảng sau đây

1. Bảng sinh viên

- Gồm các trường : rollNo, tên, tuổi

2. Bảng môn học

- mã môn học, tên môn học

3. Bảng điểm

- Mã môn học, rollNo, điểm. Biết rằng mã môn học là trường khoá ngoài vs bảng môn học, rollNo là trường khoá ngoài vs bảng sinh viên

4. Thêm cột địa chỉ, email vào bảng sinh viên 

5. Sử dụng feed để tạo ra dữ liệu mẫu. Mỗi bảng ít nhất 5 bản nghi.

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Phí Văn Long [T1907A]
Phí Văn Long

2020-06-30 10:17:43

c1,c4


<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateStudentTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('student', function (Blueprint $table) {
            $table->increments('RollNo_std');
            // $table->timestamps();
            $table->string('FullName',50);
            $table->integer('Age');
        });
        Schema::table('student', function ($table) {
            // $table->typeData('columnName');
            $table->varchar('address');
            $table->varchar('email');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('student');
    }
}
c2


<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSubjectTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('subject', function (Blueprint $table) {
            $table->increments('id_subject');
            $table->string('NameSubject');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('subject');
    }
}

c3
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePointTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('point', function (Blueprint $table) {
            $table->increments('id_point');
            $table->integer('RollNo');
            $table->float('Point');
        });
        Schema::table('point', function ($table) {
            // $table->integer('id')->unsigned();

            $table->foreign('id_point')->references('id_subject')->on('subject');
        });
         Schema::table('point', function ($table) {
            // $table->integer('RollNo')->unsigned();

            $table->foreign('RollNo')->references('RollNo_std')->on('student');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('point');
    }
}

c5
<?php

use Illuminate\Database\Seeder;

class StudentSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('student')->insert([
        	'RollNo_std'=>'1',
        	'FullName'=>'Phi Van A',
        	'Age'=>'15'
        ]);
        DB::table('student')->insert([
        	'RollNo_std'=>'2',
        	'FullName'=>'Phi Van B',
        	'Age'=>'16'
        ]);
        DB::table('student')->insert([
        	'RollNo_std'=>'3',
        	'FullName'=>'Phi Van C',
        	'Age'=>'17'
        ]);
        DB::table('student')->insert([
        	'RollNo_std'=>'4',
        	'FullName'=>'Phi Van D',
        	'Age'=>'10'
        ]);
        DB::table('student')->insert([
        	'RollNo_std'=>'5',
        	'FullName'=>'Phi Van E',
        	'Age'=>'11'
        ]);
    }
}



<?php

use Illuminate\Database\Seeder;

class SubjectSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
         DB::table('subject')->insert([
        	'id_subject'=>'1',
        	'NameSubject'=>'Toan',
        ]);
         DB::table('subject')->insert([
        	'id_subject'=>'2',
        	'NameSubject'=>'Van',
        ]);
         DB::table('subject')->insert([
        	'id_subject'=>'3',
        	'NameSubject'=>'Anh',
        ]);
         DB::table('subject')->insert([
        	'id_subject'=>'4',
        	'NameSubject'=>'Tin',
        ]);
         DB::table('subject')->insert([
        	'id_subject'=>'5',
        	'NameSubject'=>'Hoa',
        ]);
    }
}



<?php

use Illuminate\Database\Seeder;

class PointSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('point')->insert([
        	'id_point'=>'1',
        	'RollNo'=>'1',
        	'Point'=>'1.5'
        ]);
         DB::table('point')->insert([
        	'id_point'=>'2',
        	'RollNo'=>'2',
        	'Point'=>'6.5'
        ]);
          DB::table('point')->insert([
        	'id_point'=>'3',
        	'RollNo'=>'3',
        	'Point'=>'1.75'
        ]);
           DB::table('point')->insert([
        	'id_point'=>'4',
        	'RollNo'=>'4',
        	'Point'=>'10.5'
        ]);
            DB::table('point')->insert([
        	'id_point'=>'5',
        	'RollNo'=>'5',
        	'Point'=>'7.5'
        ]);
    }
}



Trương Công Vinh [T1907A]
Trương Công Vinh

2020-06-30 05:23:16



<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSubjectTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('subject', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name',100);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('subject');
    }
}



<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMarkTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('mark', function (Blueprint $table) {
            $table->unsignedInteger('std_rollno');
            $table->unsignedInteger('subject_id');
            $table->integer('mark');
            
        });
        Schema::table('mark', function (Blueprint $table) {
            $table->foreign('std_rollno')->references('rollno')->on('student');
                     
        });
        Schema::table('mark', function (Blueprint $table) {
            

            $table->foreign('subject_id')->references('id')->on('subject');            
        });
        Schema::table('student', function (Blueprint $table) {
            $table->string('email',100);
            $table->string('address',100);

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('mark');
    }
}



<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateStudentTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('student', function (Blueprint $table) {
            $table->increments('rollno');
            $table->string('name',100);
            $table->integer('age');
            
        });
        
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('student');
    }
}