多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
<article><h1>Laravel 数据库之:数据填充</h1><ul><li><a href="#introduction">简介</a></li><li><a href="#writing-seeders">编写 Seeders</a><ul><li><a href="#using-model-factories">使用模型工厂</a></li><li><a href="#calling-additional-seeders">调用其他 Seeders</a></li></ul></li><li><a href="#running-seeders">运行 Seeders</a></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p>Laravel 可以用 seed 类轻松地为数据库填充测试数据。所有的 seed 类都存放在 <code class=" language-php">database<span class="token operator">/</span>seeds</code> 目录下。你可以任意为 seed 类命名,但是应该遵守类似 <code class=" language-php">UsersTableSeeder</code> 的命名规范。Laravel 默认定义了一个 <code class=" language-php">DatabaseSeeder</code> 类。可以在这个类中使用 <code class=" language-php">call</code> 方法来运行其它的 seed 类来控制数据填充的顺序。</p><p><a name="writing-seeders"></a></p><h2><a href="#writing-seeders">编写 Seeders</a></h2><p>可以通过运行 <code class=" language-php">make<span class="token punctuation">:</span>seeder</code> <a href="/docs/5.4/artisan">Artisan 命令</a> 来生成一个 Seeder。所有由框架生成的 seeders 都将被放置在 <code class=" language-php">database<span class="token operator">/</span>seeds</code> 目录下:</p><pre class=" language-php"><code class=" language-php">php artisan make<span class="token punctuation">:</span>seeder UsersTableSeeder</code></pre><p>一个 seeder 类只包含一个默认方法:<code class=" language-php">run</code>。这个方法在 <code class=" language-php">db<span class="token punctuation">:</span>seed</code> <a href="/docs/5.4/artisan">Artisan 命令</a> 被调用时执行。在 <code class=" language-php">run</code> 方法里你可以为数据库添加任何数据。你也可以用 <a href="/docs/5.4/queries">查询语句构造器</a> 或 <a href="/docs/5.4/database-testing#writing-factories">Eloquent 模型工厂</a> 来手动添加数据。</p><p>如下所示,我们来修改默认的 <code class=" language-php">DatabaseSeeder</code> 类并为 <code class=" language-php">run</code> 方法添加一条数据库插入语句:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter">&lt;?php</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Seeder</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>Eloquent<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">DatabaseSeeder</span> <span class="token keyword">extends</span> <span class="token class-name">Seeder</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">run<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token scope">DB<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 operator">-</span><span class="token operator">&gt;</span><span class="token function">insert<span class="token punctuation">(</span></span><span class="token punctuation">[</span> <span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">str_random<span class="token punctuation">(</span></span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'email'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">str_random<span class="token punctuation">(</span></span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token string">'@gmail.com'</span><span class="token punctuation">,</span> <span class="token string">'password'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">bcrypt<span class="token punctuation">(</span></span><span class="token string">'secret'</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 punctuation">}</span></code></pre><p><a name="using-model-factories"></a></p><h3>使用模型工厂</h3><p>当然,手动为每个模型填充指定属性很麻烦。作为替代方案,你可以使用 <a href="/docs/5.4/database-testing#writing-factories">模型工厂</a> 来轻松地生成大量数据库记录。首先,阅读 <a href="/docs/5.4/database-testing#writing-factories">模型工厂文档</a> 来学习如何定义工厂。一旦定义了工厂,就可以使用 <code class=" language-php">factory</code> 这个辅助函数来向数据库中添加记录。</p><p>如下所示,我们来创建 50 个用户并为每个用户创建关联:</p><pre class=" language-php"><code class=" language-php"><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">run<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">factory<span class="token punctuation">(</span></span><span class="token scope">App<span class="token punctuation">\</span>User<span class="token punctuation">::</span></span><span class="token keyword">class</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">&gt;</span><span class="token function">create<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">each<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$u</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$u</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">posts<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">save<span class="token punctuation">(</span></span><span class="token function">factory<span class="token punctuation">(</span></span><span class="token scope">App<span class="token punctuation">\</span>Post<span class="token punctuation">::</span></span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">make<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 punctuation">}</span></code></pre><p><a name="calling-additional-seeders"></a></p><h3>调用其他 Seeders</h3><p>在 <code class=" language-php">DatabaseSeeder</code> 类中,你可以使用 <code class=" language-php">call</code> 方法来运行其他的 seed 类。为避免单个 seeder 类过大,可使用 <code class=" language-php">call</code> 方法将数据填充拆分成多个文件。只需简单传递要运行的 seeder 类名称即可:</p><pre class=" language-php"><code class=" language-php"><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">run<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">call<span class="token punctuation">(</span></span><span class="token scope">UsersTableSeeder<span class="token punctuation">::</span></span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">call<span class="token punctuation">(</span></span><span class="token scope">PostsTableSeeder<span class="token punctuation">::</span></span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">call<span class="token punctuation">(</span></span><span class="token scope">CommentsTableSeeder<span class="token punctuation">::</span></span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span></code></pre><p><a name="running-seeders"></a></p><h2><a href="#running-seeders">运行 Seeders</a></h2><p>一旦完成了 seeder 类的编写,就可以使用 <code class=" language-php">db<span class="token punctuation">:</span>seed</code> 这个 Artisan 命令来填充数据库。在默认情况下,<code class=" language-php">db<span class="token punctuation">:</span>seed</code> 命令将运行可以用来调用其他填充类的 <code class=" language-php">DatabaseSeeder</code> 类。但是可以用 <code class=" language-php"><span class="token operator">--</span><span class="token keyword">class</span></code> 选项来单独运行一个特定的 seeder 类:</p><pre class=" language-php"><code class=" language-php">php artisan db<span class="token punctuation">:</span>seed php artisan db<span class="token punctuation">:</span>seed <span class="token operator">--</span><span class="token keyword">class</span><span class="token operator">=</span>UsersTableSeeder</code></pre><p>也可以使用会先回滚再重新运行所有迁移的 <code class=" language-php">migrate<span class="token punctuation">:</span>refresh</code> 命令来填充数据库。这个命令在彻底重构数据库时非常有用:</p><pre class=" language-php"><code class=" language-php">php artisan migrate<span class="token punctuation">:</span>refresh <span class="token operator">--</span>seed</code></pre><h2>译者署名</h2><table><thead><tr><th>用户名</th><th>头像</th><th>职能</th><th>签名</th></tr></thead><tbody><tr><td><a href="https://github.com/houfei">@houfei</a></td><td><img class="avatar-66 rm-style" src="https://dn-phphub.qbox.me/uploads/avatars/3472_1452945992.jpeg?imageView2/1/w/100/h/100"></td><td>翻译</td><td>玩咖啡 &amp; 铲屎奴 &amp; 努力回头做码农 <a href="https://github.com/houfei">@houfei</a> at Github</td></tr></tbody></table></article>