Start -> Run -> Cmd
, trỏ đường dẫn đến thư mục project sunshine
c:\xampp\htdocs\sunshine
cd c:\xampp\htdocs\sunshine
artisan
để tạo MIGRATION
trong LaravelLoại
php artisan make:migration create_cusc_loai_table --create=cusc_loai
yyyy_mm_dd_hhiiss_create_loai_table
sẽ được tạo ra trong thư mục database\migrations\yyyy_mm_dd_hhiiss_create_loai_table.php
Loại
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateLoaiTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('cusc_loai', function (Blueprint $table) { $table->engine = 'InnoDB'; $table->unsignedTinyInteger('l_ma')->autoIncrement()->comment('Mã loại sản phẩm'); $table->string('l_ten', 50)->comment('Tên loại # Tên loại sản phẩm'); $table->timestamp('l_taoMoi')->default(DB::raw('CURRENT_TIMESTAMP'))->comment('Thời điểm tạo # Thời điểm đầu tiên tạo loại sản phẩm'); $table->timestamp('l_capNhat')->default(DB::raw('CURRENT_TIMESTAMP'))->comment('Thời điểm cập nhật # Thời điểm cập nhật loại sản phẩm gần nhất'); $table->tinyInteger('l_trangThai')->default('2')->comment('Trạng thái # Trạng thái loại sản phẩm: 1-khóa, 2-khả dụng'); $table->unique(['l_ten']); }); DB::statement("ALTER TABLE `cusc_loai` comment 'Loại sản phẩm # Loại sản phẩm'"); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('cusc_loai'); } }
MIGRATE
, cập nhật table Loại
vào databasephp artisan migrate
Loại
có đúng với thiết kế của Hệ thống hay không?MIGATION table LOAI
:
https://github.com/kellyfire611/sunshinenew/blob/master/database/migrations/2017_05_17_043802_create_loai_table.php
[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email)) [PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
utf8mb4_unicode_ci
thì độ dài tối đa của 1 khóa có thể chứa là 767 bytes (tương đương 191 ký tự). Khi bạn gọi hàm $table->string('column_name')
mà không chỉ định độ dài, thì Laravel sẽ báo lỗi.
$table->string()
app\Providers\AppServiceProvider.php
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema; //Fix 1071 error class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { Schema::defaultStringLength(191); //Mặc định độ dài của chuỗi là 191 ký tự } /** * Register any application services. * * @return void */ public function register() { // } }
Cùng nhau học tập, khám phá các kiến thức nền tảng về Lập trình web, mobile, database nhé.
Nền tảng kiến thức - Hành trang tới tương lai hân hạnh phục vụ Quý khách!
Khám phá, trải nghiệm ngay
Vui lòng đăng nhập để gởi bình luận!
Đăng nhậpChưa có bình luận nào!