## Laravel 5.* 執行遷移文件報錯:Specified key was too long error
命令行執行遷移文件時,報錯 `Specified key was too long`,如果我們仔細檢查錯誤會發現是唯一索引太長了,報錯信息如下:
```
[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_uniq
ue`(`email`))
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long;
max key length is 767 bytes
```
我們當然可以通過修改遷移文件來修改字段的長度,也可以通過修改遷移文件來解決這個問題。
```
$table->string('email',30)->unique();
```
也可以使用更加通用的辦法來完成。編輯 `AppServiceProvider.php` 文件的 `boot()` 方法。如下:
```
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(120);
}
```
相當于給予`varchar`默認的長度**120**。
修改數據庫的字符集
如圖,新版的Laravel 5.4 使用了 `utf8mb4` 和 `utf8mb4_unicode_ci` ,這個字符集支持 **emoji**

我們將他改成:
```
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
```
> 建議在數據庫遷移文件中使用第二個參數根據業務需求指定字段的長度,這樣更加的切合實際和需求。
- 介紹
- Laravel5發送郵件使用Service隔離業務
- 如何使用Repository模式
- 如何使用Service模式
- 如何使用Presenter模式
- Laravel 5.* 執行遷移文件報錯:Specified key was too long error
- EloquentORM關聯關系
- EloquentORM關聯關系之一對一
- EloquentORM關聯關系之一對多
- EloquentORM關聯關系之遠層一對多
- EloquentORM關聯關系之多對多
- EloquentORM關聯關系之多態關聯
- EloquentORM關聯關系之多對多多態關聯
- Laravel測試
- Laravel中涉及認證跳轉地址的修改的地方
- Laravel中Collection的基本使用
- all
- avg
- chuck
- collapse
- combine
- contains
- containsStrict
- count
- diff
- diffAssoc
- diffKeys
- each
- every
- except
- filter
- first
- flatMap
- flatten
- flip
- forget
- forPage
- get
- groupBy
- has
- implode
- intersect
- intersectKey
- isEmpty
- isNotEmpty
- keyBy
- keys
- last
- map
- mapWithKeys
- max
- median
- merge
- min
- mode
- nth
- only
- partition
- pipe
- pluck
- pop
- prepend
- pull
- push
- put
- random
- reduce
- reject
- reverse
- search
- shift
- shuffle
- slice
- sort
- sortBy
- sortByDesc
- splice
- split
- sum
- take
- tap
- times
- toArray
- toJson
- transform
- union
- unique
- uniqueStrict
- values
- when
- where
- whereStrict
- whereIn
- whereInStrict
- whereNotIn
- whereNotInStrict
- zip
- Laravel中Collection的實際使用
- collection中sum求和
- collection格式化計算數據
- collection格式化計算數據計算github事件得分總和
- collection格式化markdown數據列表
- collection格式化計算兩個數組的數據
- collection中reduce創建lookup數組
- TODO