<article><h1>Laravel 的数据库迁移 Migrations</h1><ul><li><a href="#introduction">简介</a></li><li><a href="#generating-migrations">生成迁移</a></li><li><a href="#migration-structure">迁移结构</a></li><li><a href="#running-migrations">运行迁移</a><ul><li><a href="#rolling-back-migrations">回滚迁移</a></li></ul></li><li><a href="#tables">数据表</a><ul><li><a href="#creating-tables">创建数据表</a></li><li><a href="#renaming-and-dropping-tables">重命名与删除数据表</a></li></ul></li><li><a href="#columns">字段</a><ul><li><a href="#creating-columns">创建字段</a></li><li><a href="#column-modifiers">字段修饰</a></li><li><a href="#modifying-columns">修改字段</a></li><li><a href="#dropping-columns">移除字段</a></li></ul></li><li><a href="#indexes">索引</a><ul><li><a href="#creating-indexes">创建索引</a></li><li><a href="#dropping-indexes">删除索引</a></li><li><a href="#foreign-key-constraints">外键约束</a></li></ul></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p>数据库迁移就像是数据库的版本控制,可以让你的团队轻松修改并共享应用程序的数据库结构。迁移通常会搭配上 Laravel 的数据库结构构造器来让你方便地构建数据库结构。如果你曾经出现过让同事手动在数据库结构中添加字段的情况,数据库迁移可以解决你这个问题。</p><p>Laravel 的 <code class=" language-php">Schema</code> <a href="/docs/5.4/facades">facade</a> 对所有 Laravel 支持的数据库系统提供了创建和操作数据表的相应支持。</p><p><a name="generating-migrations"></a></p><h2><a href="#generating-migrations">生成迁移</a></h2><p>使用 <code class=" language-php">make<span class="token punctuation">:</span>migration</code> <a href="/docs/5.4/artisan">Artisan 命令</a> 来创建迁移:</p><pre class=" language-php"><code class=" language-php">php artisan make<span class="token punctuation">:</span>migration create_users_table</code></pre><p>新的迁移文件将会被放置在 <code class=" language-php">database<span class="token operator">/</span>migrations</code> 目录中。每个迁移文件的名称都包含了一个时间戳,以便让 Laravel 确认迁移的顺序。</p><p><code class=" language-php"><span class="token operator">--</span>table</code> 和 <code class=" language-php"><span class="token operator">--</span>create</code> 选项可用来指定数据表的名称,或是该迁移被执行时会创建的新数据表。这些选项需在预生成迁移文件时填入指定的数据表:</p><pre class=" language-php"><code class=" language-php">php artisan make<span class="token punctuation">:</span>migration create_users_table <span class="token operator">--</span>create<span class="token operator">=</span>users
php artisan make<span class="token punctuation">:</span>migration add_votes_to_users_table <span class="token operator">--</span>table<span class="token operator">=</span>users</code></pre><p>如果你想为生成的迁移指定一个自定义输出路径,则可以在运行 <code class=" language-php">make<span class="token punctuation">:</span>migration</code> 命令时添加 <code class=" language-php"><span class="token operator">--</span>path</code> 选项。提供的路径必须是相对于应用程序的基本路径。</p><p><a name="migration-structure"></a></p><h2><a href="#migration-structure">迁移结构</a></h2><p>一个迁移类会包含两个方法: <code class=" language-php">up</code> 和 <code class=" language-php">down</code> 。 <code class=" language-php">up</code> 方法可为数据库添加新的数据表、字段或索引,而 <code class=" language-php">down</code> 方法则是 <code class=" language-php">up</code> 方法的逆操作。</p><p>你可以在这两个方法中使用 Laravel 数据库结构构造器来创建以及修改数据表。若要了解 <code class=" language-php">数据库结构</code> 构造器中的所有可用方法,<a href="#creating-tables">可查阅它的文档</a>。以下的迁移实例会创建一张 <code class=" language-php">flights</code> 数据表:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Migrations<span class="token punctuation">\</span>Migration</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">CreateFlightsTable</span> <span class="token keyword">extends</span> <span class="token class-name">Migration</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* 运行数据库迁移
*
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">up<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">create<span class="token punctuation">(</span></span><span class="token string">'flights'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>Blueprint <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">increments<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">string<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">string<span class="token punctuation">(</span></span><span class="token string">'airline'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">timestamps<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">/**
* 回滚数据库迁移
*
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">down<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">drop<span class="token punctuation">(</span></span><span class="token string">'flights'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p><a name="running-migrations"></a></p><h2><a href="#running-migrations">运行迁移</a></h2><p>使用 <code class=" language-php">migrate</code> Artisan 命令,来运行所有未运行过的迁移:</p><pre class=" language-php"><code class=" language-php">php artisan migrate</code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 如果你使用的是 <a href="/docs/5.4/homestead">Homestead 虚拟机</a> , 你需要在虚拟机中执行以上命令。</p></blockquote><h4>在线上环境强制执行迁移</h4><p>一些迁移的操作是具有破坏性的,它们可能会导致数据丢失。为了保护线上环境的数据库,系统会在这些命令被运行之前显示确认提示。若要忽略此提示并强制运行命令,则可以使用 <code class=" language-php"><span class="token operator">--</span>force</code> 标记:</p><pre class=" language-php"><code class=" language-php">php artisan migrate <span class="token operator">--</span>force</code></pre><p><a name="rolling-back-migrations"></a></p><h3>回滚迁移</h3><p>若要回滚最后一次迁移,则可以使用 <code class=" language-php">rollback</code> 命令。此命令是对上一次执行的「批量」迁移回滚,其中可能包括多个迁移文件:</p><pre class=" language-php"><code class=" language-php">php artisan migrate<span class="token punctuation">:</span>rollback</code></pre><p>在 <code class=" language-php">rollback</code> 命令后加上 <code class=" language-php">step</code> 参数,你可以限制回滚迁移的个数。例如,下面的命令将会回滚最后的 5 个迁移。</p><pre class=" language-php"><code class=" language-php">php artisan migrate<span class="token punctuation">:</span>rollback <span class="token operator">--</span>step<span class="token operator">=</span><span class="token number">5</span></code></pre><p><code class=" language-php">migrate<span class="token punctuation">:</span>reset</code> 命令可以回滚应用程序中的所有迁移:</p><pre class=" language-php"><code class=" language-php">php artisan migrate<span class="token punctuation">:</span>reset</code></pre><h4>使用单个命令来执行回滚或迁移</h4><p><code class=" language-php">migrate<span class="token punctuation">:</span>refresh</code> 命令不仅会回滚数据库的所有迁移还会接着运行 <code class=" language-php">migrate</code> 命令。所以此命令可以有效的重新创建整个数据库:</p><pre class=" language-php"><code class=" language-php">php artisan migrate<span class="token punctuation">:</span>refresh
<span class="token comment" spellcheck="true">
// 刷新数据库结构并执行数据填充
</span>php artisan migrate<span class="token punctuation">:</span>refresh <span class="token operator">--</span>seed</code></pre><p>使用 <code class=" language-php">refresh</code> 命令并加上 <code class=" language-php">step</code> 参数,你也可以限制执行回滚和再迁移的个数。比如,下面的命令会回滚并再迁移最后的 5 个迁移:</p><pre class=" language-php"><code class=" language-php">php artisan migrate<span class="token punctuation">:</span>refresh <span class="token operator">--</span>step<span class="token operator">=</span><span class="token number">5</span></code></pre><p><a name="tables"></a></p><h2><a href="#tables">数据表</a></h2><p><a name="creating-tables"></a></p><h3>创建数据表</h3><p>要创建一张新的数据表,可以使用 <code class=" language-php">Schema</code> facade 的 <code class=" language-php">create</code> 方法。<code class=" language-php">create</code> 方法接收两个参数:第一个参数为数据表的名称,第二个参数为一个 <code class=" language-php">闭包</code> ,此闭包会接收一个用于定义新数据表的 <code class=" language-php">Blueprint</code> 对象:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">create<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>Blueprint <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">increments<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>当然,在创建数据表的时候,你也可以使用任何数据库结构构造器的 <a href="#creating-columns">字段方法</a> 来定义数据表的字段。</p><h4>检查数据表或字段是否存在</h4><p>你可以方便地使用 <code class=" language-php">hasTable</code> 和 <code class=" language-php">hasColumn</code> 方法来检查数据表或字段是否存在:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">hasTable<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">hasColumn<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">,</span> <span class="token string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span></code></pre><h4>数据库连接与存储引擎</h4><p>如果你想要在一个非默认的数据库连接中进行数据库结构操作,可以使用 <code class=" language-php">connection</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">connection<span class="token punctuation">(</span></span><span class="token string">'foo'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">create<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>Blueprint <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">increments<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你可以在数据库结构构造器上设置 <code class=" language-php">engine</code> 属性来设置数据表的存储引擎:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">create<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>Blueprint <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">engine</span> <span class="token operator">=</span> <span class="token string">'InnoDB'</span><span class="token punctuation">;</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">increments<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="renaming-and-dropping-tables"></a></p><h3>重命名与删除数据表</h3><p>若要重命名一张已存在的数据表,可以使用 <code class=" language-php">rename</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">rename<span class="token punctuation">(</span></span><span class="token variable">$from</span><span class="token punctuation">,</span> <span class="token variable">$to</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>要删除已存在的数据表,可使用 <code class=" language-php">drop</code> 或 <code class=" language-php">dropIfExists</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">drop<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">dropIfExists<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>重命名带外键的数据表</h4><p>在重命名前,你需要检查外键的约束涉及到的数据表名需要在迁移文件中显式的提供,而不是让 Laravel 按照约定来设置一个名称。因为那样会让外键约束关联到旧的数据表上。</p><p><a name="columns"></a></p><h2><a href="#columns">字段</a></h2><p><a name="creating-columns"></a></p><h3>创建字段</h3><p>使用 <code class=" language-php">Schema</code> facade 的 <code class=" language-php">table</code> 方法可以更新已有的数据表。如同 <code class=" language-php">create</code> 方法,<code class=" language-php">table</code> 方法会接收两个参数:一个是数据表的名称,另一个则是接收 <code class=" language-php">Blueprint</code> 实例的<code class=" language-php">闭包</code>。我们可以使用它来为数据表新增字段:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">table<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>Blueprint <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">string<span class="token punctuation">(</span></span><span class="token string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>可用的字段类型</h4><p>数据库结构构造器包含了许多字段类型,供你构建数据表时使用:</p><table><thead><tr><th>命令</th><th>描述</th></tr></thead><tbody><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">bigIncrements<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>递增 ID(主键),相当于「UNSIGNED BIG INTEGER」型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">bigInteger<span class="token punctuation">(</span></span><span class="token string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 BIGINT 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">binary<span class="token punctuation">(</span></span><span class="token string">'data'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 BLOB 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">boolean<span class="token punctuation">(</span></span><span class="token string">'confirmed'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 BOOLEAN 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">char<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 CHAR 型态,并带有长度。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">date<span class="token punctuation">(</span></span><span class="token string">'created_at'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 DATE 型态</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">dateTime<span class="token punctuation">(</span></span><span class="token string">'created_at'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 DATETIME 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">dateTimeTz<span class="token punctuation">(</span></span><span class="token string">'created_at'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>DATETIME (带时区) 形态</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">decimal<span class="token punctuation">(</span></span><span class="token string">'amount'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 DECIMAL 型态,并带有精度与基数。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">double<span class="token punctuation">(</span></span><span class="token string">'column'</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 DOUBLE 型态,总共有 15 位数,在小数点后面有 8 位数。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">enum<span class="token punctuation">(</span></span><span class="token string">'choices'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'foo'</span><span class="token punctuation">,</span> <span class="token string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 ENUM 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">float<span class="token punctuation">(</span></span><span class="token string">'amount'</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 FLOAT 型态,总共有 8 位数,在小数点后面有 2 位数。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">increments<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>递增的 ID (主键),使用相当于「UNSIGNED INTEGER」的型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">integer<span class="token punctuation">(</span></span><span class="token string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 INTEGER 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">ipAddress<span class="token punctuation">(</span></span><span class="token string">'visitor'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 IP 地址形态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">json<span class="token punctuation">(</span></span><span class="token string">'options'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 JSON 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">jsonb<span class="token punctuation">(</span></span><span class="token string">'options'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 JSONB 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">longText<span class="token punctuation">(</span></span><span class="token string">'description'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 LONGTEXT 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">macAddress<span class="token punctuation">(</span></span><span class="token string">'device'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 MAC 地址形态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">mediumIncrements<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>递增 ID (主键) ,相当于「UNSIGNED MEDIUM INTEGER」型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">mediumInteger<span class="token punctuation">(</span></span><span class="token string">'numbers'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 MEDIUMINT 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">mediumText<span class="token punctuation">(</span></span><span class="token string">'description'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 MEDIUMTEXT 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">morphs<span class="token punctuation">(</span></span><span class="token string">'taggable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>加入整数 <code class=" language-php">taggable_id</code> 与字符串 <code class=" language-php">taggable_type</code>。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">nullableMorphs<span class="token punctuation">(</span></span><span class="token string">'taggable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>与 <code class=" language-php"><span class="token function">morphs<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code> 字段相同,但允许为NULL。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">nullableTimestamps<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>与 <code class=" language-php"><span class="token function">timestamps<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code> 相同,但允许为 NULL。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">rememberToken<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>加入 <code class=" language-php">remember_token</code> 并使用 VARCHAR(100) NULL。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">smallIncrements<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>递增 ID (主键) ,相当于「UNSIGNED SMALL INTEGER」型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">smallInteger<span class="token punctuation">(</span></span><span class="token string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 SMALLINT 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">softDeletes<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>加入 <code class=" language-php">deleted_at</code> 字段用于软删除操作。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">string<span class="token punctuation">(</span></span><span class="token string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 VARCHAR 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">string<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 VARCHAR 型态,并带有长度。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">text<span class="token punctuation">(</span></span><span class="token string">'description'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 TEXT 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">time<span class="token punctuation">(</span></span><span class="token string">'sunrise'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 TIME 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">timeTz<span class="token punctuation">(</span></span><span class="token string">'sunrise'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 TIME (带时区) 形态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">tinyInteger<span class="token punctuation">(</span></span><span class="token string">'numbers'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 TINYINT 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">timestamp<span class="token punctuation">(</span></span><span class="token string">'added_on'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 TIMESTAMP 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">timestampTz<span class="token punctuation">(</span></span><span class="token string">'added_on'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 TIMESTAMP (带时区) 形态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">timestamps<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>加入 <code class=" language-php">created_at</code> 和 <code class=" language-php">updated_at</code> 字段。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">timestampsTz<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>加入 <code class=" language-php">created_at</code> and <code class=" language-php">updated_at</code> (带时区) 字段,并允许为NULL。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">unsignedBigInteger<span class="token punctuation">(</span></span><span class="token string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 Unsigned BIGINT 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">unsignedInteger<span class="token punctuation">(</span></span><span class="token string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 Unsigned INT 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">unsignedMediumInteger<span class="token punctuation">(</span></span><span class="token string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 Unsigned MEDIUMINT 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">unsignedSmallInteger<span class="token punctuation">(</span></span><span class="token string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 Unsigned SMALLINT 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">unsignedTinyInteger<span class="token punctuation">(</span></span><span class="token string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 Unsigned TINYINT 型态。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">uuid<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>相当于 UUID 型态。</td></tr></tbody></table><p><a name="column-modifiers"></a></p><h3>字段修饰</h3><p>除了上述的字段类型列表,还有一些其它的字段「修饰」,你可以将它增加到字段中。例如,若要让字段「nullable」,那么你可以使用 <code class=" language-php">nullable</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">table<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>Blueprint <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">string<span class="token punctuation">(</span></span><span class="token string">'email'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">nullable<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>以下列表为字段的可用修饰。此列表不包括 <a href="#creating-indexes">索引修饰</a>:</p><table><thead><tr><th>Modifier</th><th>Description</th></tr></thead><tbody><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">after<span class="token punctuation">(</span></span><span class="token string">'column'</span><span class="token punctuation">)</span></code></td><td>将此字段放置在其它字段「之后」(仅限 MySQL)</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">comment<span class="token punctuation">(</span></span><span class="token string">'my comment'</span><span class="token punctuation">)</span></code></td><td>增加注释</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token keyword">default</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span></code></td><td>为此字段指定「默认」值</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">first<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></td><td>将此字段放置在数据表的「首位」(仅限 MySQL)</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">nullable<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></td><td>此字段允许写入 NULL 值</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">storedAs<span class="token punctuation">(</span></span><span class="token variable">$expression</span><span class="token punctuation">)</span></code></td><td>创建一个存储的生成字段 (仅限 MySQL)</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">unsigned<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></td><td>设置 <code class=" language-php">integer</code> 字段为 <code class=" language-php"><span class="token constant">UNSIGNED</span></code></td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">virtualAs<span class="token punctuation">(</span></span><span class="token variable">$expression</span><span class="token punctuation">)</span></code></td><td>创建一个虚拟的生成字段 (仅限 MySQL)</td></tr></tbody></table><p><a name="changing-columns"></a> <a name="modifying-columns"></a></p><h3>修改字段</h3><h4>先决条件</h4><p>在修改字段之前,请务必在你的 <code class=" language-php">composer<span class="token punctuation">.</span>json</code> 中增加 <code class=" language-php">doctrine<span class="token operator">/</span>dbal</code> 依赖。Doctrine DBAL 函数库被用于判断当前字段的状态以及创建调整指定字段的 SQL 查询。</p><pre class=" language-php"><code class=" language-php">composer <span class="token keyword">require</span> doctrine<span class="token operator">/</span>dbal</code></pre><h4>更新字段属性</h4><p><code class=" language-php">change</code> 方法让你可以修改一些已存在的字段类型,或修改字段属性。比如,你可能想增加字符串字段的长度。想了解 <code class=" language-php">change</code> 方法如何使用,让我们来把 <code class=" language-php">name</code> 字段的长度从 25 增加到 50 :</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">table<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>Blueprint <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">string<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">change<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>我们也能将字段修改为 nullable:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">table<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>Blueprint <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">string<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">nullable<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">change<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 下面的字段类型不能被「修改」: char,double,enum,mediumInteger,timestamp,tinyInteger,ipAddress,json,jsonb,macAddress,mediumIncrements,morphs,nullableMorphs,nullableTimestamps,softDeletes,timeTz,timestampTz,timestamps,timestampsTz,unsignedMediumInteger,unsignedTinyInteger,uuid。</p></blockquote><p><a name="renaming-columns"></a></p><h4>重命名字段</h4><p>要重命名字段,可使用数据库结构构造器的 <code class=" language-php">renameColumn</code> 方法。在重命名字段前,请确保你的 <code class=" language-php">composer<span class="token punctuation">.</span>json</code> 文件内已经加入 <code class=" language-php">doctrine<span class="token operator">/</span>dbal</code> 依赖:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">table<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>Blueprint <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">renameColumn<span class="token punctuation">(</span></span><span class="token string">'from'</span><span class="token punctuation">,</span> <span class="token string">'to'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 数据表的 <code class=" language-php">enum</code> 字段暂时不支持修改字段属性。</p></blockquote><p><a name="dropping-columns"></a></p><h3>移除字段</h3><p>要移除字段,可使用数据库结构构造器的 <code class=" language-php">dropColumn</code> 方法。在删除 SQLite 数据库的字段前,你需要在 <code class=" language-php">composer<span class="token punctuation">.</span>json</code> 文件中加入 <code class=" language-php">doctrine<span class="token operator">/</span>dbal</code> 依赖并在终端执行 <code class=" language-php">composer update</code> 来安装函数库:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">table<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>Blueprint <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">dropColumn<span class="token punctuation">(</span></span><span class="token string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你可以传递多个字段的数组至 <code class=" language-php">dropCloumn</code> 方法来移除多个字段:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">table<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>Blueprint <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">dropColumn<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'votes'</span><span class="token punctuation">,</span> <span class="token string">'avatar'</span><span class="token punctuation">,</span> <span class="token string">'location'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> SQLite 数据库并不支持在单个迁移中移除或修改多个字段。</p></blockquote><p><a name="indexes"></a></p><h2><a href="#indexes">索引</a></h2><p><a name="creating-indexes"></a></p><h3>创建索引</h3><p>数据库结构构造器支持多种类型的索引。首先,让我们先来看一个示例,其指定了字段的值必须是唯一的。你可以简单的在字段定义之后链式调用 <code class=" language-php">unique</code> 方法来创建索引:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">string<span class="token punctuation">(</span></span><span class="token string">'email'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">unique<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>此外,你也可以在定义完字段之后创建索引。例如:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">unique<span class="token punctuation">(</span></span><span class="token string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你也可以传递一个字段的数组至索引方法来创建复合索引:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">index<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'account_id'</span><span class="token punctuation">,</span> <span class="token string">'created_at'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>Laravel 会自动生成一个合理的索引名称,但你也可以使用第二个参数来自定义索引名称:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">index<span class="token punctuation">(</span></span><span class="token string">'email'</span><span class="token punctuation">,</span> <span class="token string">'my_index_name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>可用的索引类型</h4><table><thead><tr><th>Command</th><th>Description</th></tr></thead><tbody><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">primary<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>加入主键。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">primary<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'first'</span><span class="token punctuation">,</span> <span class="token string">'last'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>加入复合键。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">unique<span class="token punctuation">(</span></span><span class="token string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>加入唯一索引。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">unique<span class="token punctuation">(</span></span><span class="token string">'state'</span><span class="token punctuation">,</span> <span class="token string">'my_index_name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>自定义索引名称。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">unique<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'first'</span><span class="token punctuation">,</span> <span class="token string">'last'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>加入复合唯一键。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">index<span class="token punctuation">(</span></span><span class="token string">'state'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>加入基本索引。</td></tr></tbody></table><h4>索引长度 & MySQL / MariaDB</h4><p>Laravel 默认使用 <code class=" language-php">utf8mb4</code> 字符,包括支持在数据库存储「表情」。如果你正在运行的 MySQL release 版本低于5.7.7 或 MariaDB release 版本低于10.2.2 ,为了MySQL为它们创建索引,你可能需要手动配置迁移生成的默认字符串长度,你可以通过调用 <code class=" language-php">AppServiceProvider</code> 中的 <code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span>defaultStringLength</code> 方法来配置它:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/**
* 引导任何应用程序服务。
*
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">boot<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">defaultStringLength<span class="token punctuation">(</span></span><span class="token number">191</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p>或者你可以为数据库开启 <code class=" language-php">innodb_large_prefix</code> 选项,有关如何正确开启此选项的说明请查阅数据库文档。</p><p><a name="dropping-indexes"></a></p><h3>移除索引</h3><p>若要移除索引,则必须指定索引的名称。Laravel 默认会自动给索引分配合理的名称。其将数据表名称、索引的字段名称及索引类型简单地连接在了一起。举例如下:</p><table><thead><tr><th>命令</th><th>描述</th></tr></thead><tbody><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">dropPrimary<span class="token punctuation">(</span></span><span class="token string">'users_id_primary'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>从「users」数据表移除主键。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">dropUnique<span class="token punctuation">(</span></span><span class="token string">'users_email_unique'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>从「users」数据表移除唯一索引。</td></tr><tr><td><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">dropIndex<span class="token punctuation">(</span></span><span class="token string">'geo_state_index'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>从「geo」数据表移除基本索引。</td></tr></tbody></table><p>如果你对 <code class=" language-php">dropIndex</code> 传参索引数组,默认的约定是索引名称由数据库表名字和键名拼接而成:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">table<span class="token punctuation">(</span></span><span class="token string">'geo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>Blueprint <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">dropIndex<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'state'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true"> // 移除索引 'geo_state_index'
</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="foreign-key-constraints"></a></p><h3>外键约束</h3><p>Laravel 也为创建外键约束提供了支持,用于在数据库层中的强制引用完整性。例如,让我们定义一个有 <code class=" language-php">user_id</code> 字段的 <code class=" language-php">posts</code> 数据表,<code class=" language-php">user_id</code> 引用了 <code class=" language-php">users</code> 数据表的 <code class=" language-php">id</code> 字段:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">table<span class="token punctuation">(</span></span><span class="token string">'posts'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>Blueprint <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">integer<span class="token punctuation">(</span></span><span class="token string">'user_id'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">unsigned<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">foreign<span class="token punctuation">(</span></span><span class="token string">'user_id'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">references<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">on<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你也可以指定约束的「on delete」及「on update」:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">foreign<span class="token punctuation">(</span></span><span class="token string">'user_id'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">references<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">on<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">onDelete<span class="token punctuation">(</span></span><span class="token string">'cascade'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>要移除外键,你可以使用 <code class=" language-php">dropForeign</code> 方法。外键约束与索引采用相同的命名方式。所以,我们可以将数据表名称和约束字段连接起来,接着在该名称后面加上「_foreign」后缀:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">dropForeign<span class="token punctuation">(</span></span><span class="token string">'posts_user_id_foreign'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你也可以传递一个包含字段的数组,在移除的时候字段会按照惯例被自动转换为对应的外键名称:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">dropForeign<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'user_id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你可以在迁移文件里使用以下方法来开启和关闭外键约束:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">enableForeignKeyConstraints<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">disableForeignKeyConstraints<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h2>译者署名</h2><table><thead><tr><th>用户名</th><th>头像</th><th>职能</th><th>签名</th></tr></thead><tbody><tr><td><a href="https://laravel-china.org/users/5336">@em0t</a></td><td><img class="avatar-66 rm-style" src="https://dn-phphub.qbox.me/uploads/avatars/5336_1470812559.jpeg?imageView2/1/w/100/h/100"></td><td>翻译</td><td>coding is funny</td></tr></tbody></table></article>
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Valet
- Homestead
- 核心概念
- 服务提供者
- Facades
- Contracts
- 服务容器
- HTTP 层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- API 认证
- 用户认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- Redis
- 数据填充
- Eloquent ORM
- Eloquent ORM快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度