🧐[COMMON] Schema để thao tác với Database, foreign (ok)

Ví dụ 1

**RolesPermission migration table**

Schema::create('roles', function(Blueprint $table){
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('label');
            $table->string('description')->nullable();
            $table->timestamps();            
        });
        
        Schema::create('permissions', function(Blueprint $table){
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('label');
            $table->string('description')->nullable();
            $table->timestamps();            
        });
        
        Schema::create('permission_role', function(Blueprint $table){
            $table->integer('permission_id')->unsigned();
            $table->integer('role_id')->unsigned();
            
            $table->foreign('permission_id')
                    ->references('id')
                    ->on('permissions')
                    ->onDelete('cascade');
            
            $table->foreign('role_id')
                    ->references('id')
                    ->on('roles')
                    ->onDelete('cascade');
            
            $table->primary(['permission_id', 'role_id']);
        });
        
        Schema::create('role_user', function(Blueprint $table){
            $table->integer('role_id')->unsigned();
            $table->integer('user_id')->unsigned();
            
            $table->foreign('role_id')
                    ->references('id')
                    ->on('roles')
                    ->onDelete('cascade');
            
            $table->foreign('user_id')
                    ->references('id')
                    ->on('users')
                    ->onDelete('cascade');
            
            $table->primary(['role_id', 'user_id']);
            
        });


.env file
APP_ENV=local
APP_DEBUG=true
APP_KEY=W8YWZe3LCngvZzexH3WLWqCDlYRSufuy

DB_HOST=127.0.0.1
DB_DATABASE=testing
DB_USERNAME=root
DB_PASSWORD=

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=log
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

Ví dụ 2 (sử dụng primary key không phải là id)

Chú ý sử dụng $table->increments('category_id') => category_id sẽ ở dạng unsigned integer do đó để đặt giống kiểu dữ liệu $table->unsignedInteger('category_id') nếu không sẽ lỗi 😒

database\migrations\2022_08_17_085654_create_tbl_product_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTblProductTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tbl_category', function (Blueprint $table) {
            $table->increments('category_id');
            // $table->id();
            $table->string('category_name');
            $table->string('category_des');
            $table->integer('category_status');
            $table->timestamps();
        });
        Schema::create('tbl_product', function (Blueprint $table) {
            $table->increments('product_id');
            // $table->increments('id');
            // $table->id();
            // $table->integer('brand_id');
            $table->string('product_img');
            $table->string('product_title');
            // $table->string('product_content');
            // $table->integer('product_price');
            // $table->string('product_status');
            // $table->timestamps();
            $table->unsignedInteger('category_id');
            // // $table->integer('category_id');
            $table->foreign('category_id')->references('category_id')->on('tbl_category');
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tbl_category');
        Schema::dropIfExists('tbl_product');
    }
}

Last updated