# laravel migrate
## 即使在遷移時設置了默認值,批量分配也不會處理空輸入
https://stackoverflow.com/questions/56353195/mass-assignment-wont-handle-null-input-even-when-default-is-set-on-migration-an
## Laravel 7+ 的外键
从 Laravel 7 开始,你不需要在迁移(migration)中为一些关系字段写两行代码 —— 一行是字段,一行是外键。你可以使用 foreignId() 方法。
```
// Laravel 7 之前
Schema::table('posts', function (Blueprint $table)) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
}
// 从 Laravel 7 开始
Schema::table('posts', function (Blueprint $table)) {
$table->foreignId('user_id')->constrained();
}
// 或者你的字段不同于表中的引用的
Schema::table('posts', function (Blueprint $table)) {
$table->foreignId('created_by_id')->constrained('users', 'column');
}
```
## 組合key

laravel unique array是組合key
他不是分開兩個建立
## primary

多態primary常用

## 外key也是index
## 常遇到錯誤
當表錯誤
rollback
還是會說表存在
解決方法
刪除那個表
在我寫的控制台中 php artisan tinker
然後,再次在控制台中, Schema::drop('users')
最後php artisan migrate,一切都奏效了。
## unique
unique 也會建立index
`$table->string('email')->unique();`
此外,你可以在定義完欄位之後建立索引。例如:
`$table->unique('email');`
你也可以傳遞一個欄位的陣列至索引方法來建立複合索引:
`$table->index(['account_id', 'created_at']);`
但可能會爆開(name沒這麼長)
因為index會需要一個名稱
所以$table->unique(['email','good'])
這樣她會自動把兩個加起來
解決辦法
幫他設一個名稱就好了
https://laraveldaily.com/migration-index-too-long-choose-the-name-yourself/
## 外key約束
ON DELETE
restrict(約束):當在父表(即外來鍵的來源表)中刪除對應記錄時,首先檢查該記錄是否有對應外來鍵,如果有則不允許刪除。
no action:意思同restrict.即如果存在從資料,不允許刪除主資料。
cascade(級聯):當在父表(即外來鍵的來源表)中刪除對應記錄時,首先檢查該記錄是否有對應外來鍵,如果有則也刪除外來鍵在子表(即包含外來鍵的表)中的記錄。
set null:當在父表(即外來鍵的來源表)中刪除對應記錄時,首先檢查該記錄是否有對應外來鍵,如果有則設定子表中該外來鍵值為null(不過這就要求該外來鍵允許取null)
## forgin key失敗 修正之後alreay
https://www.youtube.com/watch?v=DWzUBpsEEHY
第一個常用 刪除表 重跑
第二個 改成table

## after before
只有在mysql
```
Schema::table('users', function (Blueprint $table) {
$table->string('phone')->after('email');
});
```
```
Schema::table('users', function (Blueprint $table) {
$table->string('phone')->before('created_at');
});
```
```
Schema::table('users', function (Blueprint $table) {
$table->string('uuid')->first();
});
```
## unsigned()
小心,unsigned()!
(2) 『當外鍵有參照到自動增量時,記得設定外鍵為 unsigned 型態。』- from http://laravel.tw/docs/4.2/schema
這句話的意思是,如果某張表的 key 是參考到另一張表的鍵而且是 auto_increment 時,要記得將那個外鍵設定為 unsigned()
範例,假這個 id 會在別的 table 某欄位參考到,那麼就要記得 unsigned:
$table->increments('id')->unsigned();
**表有更動都要composer dump-autoload**
## 建表的雷
請先建立沒有外key的
錯了還是會建立表格 可以手動刪除或跑指令
先刪除外key(技術長說不用)

## 外key可以用array

## 實用技巧
https://www.youtube.com/watch?v=bSQcmcu6yHc
**第一**
id 預設big 不然就把預設的改increments
**第二**
用外key的 要先null再跑constrained
**第三**
外key記得要 ondelete 不然會被約束
**第四**
改動表單時間 可以排序
**第五**
要看migrate紀錄 用 php artisan migrate:status
**第六**
時間預設 timestap 用 useCurrentOnUpdate()
**第七**
刪除 dropColum 那邊可以用陣列 dropColum(['sss','bbb']);
**第八**
rollback refresh 可以回指定 php artisan migrate:rollback --stup=3
**第九**
from 可以指定開始的 id->from(100) 從一百開始數
**第十** 超好用
php artisan make:migration add_some_file_to_user_table
可用 ""打
php artisan make:migration "add some file to user table"
## 欄位
int 只能到 11
可以用
**tinyInteger()**
該tinyInteger方法創建一個TINYINT等效列:
$table->tinyInteger('votes');
## 外key約束
外鍵約束
Laravel 還支持創建用於在資料庫層中的強制引用完整性的外鍵約束。例如,讓我們在 posts 表格上定義一個參考到 users 表格的 id 欄位的 user_id 欄位:
```
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::table('posts', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
```
由於這種外鍵約束的定義方式過於繁複,Laravel 額外提供了更簡潔的方法來提供更好的開發人員體驗,因此上面的範例還可以這麼寫:
```
Schema::table('posts', function (Blueprint $table) {
$table->foreignId('user_id')->constrained();
});
```
foreignId() 是 unsignedBigInteger 的別名,而 constrained() 將使用命名慣例來確定所參考的表格名和欄位名。如果表格名與命名慣例不匹配,可以通過將表格名作為參數傳遞給 constrained() 來指定表格名:
```
Schema::table('posts', function (Blueprint $table) {
$table->foreignId('user_id')->constrained('users');
});
```
## 多態
就是多態的意思
一個很多人用

你可以看到他做的
id跟type
還有
index
是兩個都有加上index喔 注意
因為type是string
所以index可能會爆開
在mySql爆開 你就回去看一多態
### 關係
一對一
morphTo
morphOne
一對多
morphTo
morphMany
多對多
沒有morphTo這樣去定義
morphBymany去定義
反向跟morphBymany第二參數都是關聯表
多對多一般都用trait
然後trait的function
可以直接啟動 因為boot方法
去看model那篇
### morphMap
###### tags: `Laravel`