佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > Laravel框架使用Seeder實(shí)現(xiàn)自動(dòng)填充數(shù)據(jù)功能

Laravel框架使用Seeder實(shí)現(xiàn)自動(dòng)填充數(shù)據(jù)功能

熱門(mén)標(biāo)簽:Mysql連接數(shù)設(shè)置 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 電子圍欄 服務(wù)器配置 Linux服務(wù)器 團(tuán)購(gòu)網(wǎng)站 銀行業(yè)務(wù) 阿里云

本文實(shí)例講述了Laravel框架使用Seeder實(shí)現(xiàn)自動(dòng)填充數(shù)據(jù)功能。分享給大家供大家參考,具體如下:

要查看代碼,可以點(diǎn)擊鏈接:https://github.com/laravel/framework

Laravel自動(dòng)填充數(shù)據(jù)使用的是Seeder類(lèi)

?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
  /**
   * Run the database seeds.
   */
  public function run()
  {
    //
  }
}
class MyTableSeeder extends Seeder
{
  public function run()
  {
    //
  }
}

你自定義的Seeder只有一個(gè)run函數(shù),里面寫(xiě)你的自動(dòng)填充步驟

大家會(huì)注意到這兩個(gè)函數(shù)

Model::unguard();
//你的填充操作
Model::reguard();

曾經(jīng)對(duì)這兩個(gè)函數(shù)非常疑惑,到底是干什么用的,只能推測(cè)是一對(duì)互為反作用的函數(shù)。于是去查了下源代碼。

在目錄\vendor\laravel\framework\src\Illuminate\Database\Eloquent下的Model.php下定義了這兩個(gè)函數(shù)

/**
* Disable all mass assignable restrictions.
*
* @param bool $state
* @return void
*/
public static function unguard($state = true)
{
    static::$unguarded = $state;
}
/**
* Enable the mass assignment restrictions.
*
* @return void
*/
public static function reguard()
{
    static::$unguarded = false;
}

看Laravel作者的注釋可以知道,是對(duì)數(shù)據(jù)填充限制的操作。

所以u(píng)nguard在前,reguard在后,unguard負(fù)責(zé)解除自動(dòng)填充操作限制,reguard負(fù)責(zé)恢復(fù)限制。

在填充操作之前,建議使用模型的成員函數(shù)  

Model::truncate();

這個(gè)函數(shù)會(huì)清空這個(gè)模型所對(duì)應(yīng)的數(shù)據(jù)表,所以請(qǐng)慎重使用。

?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
  /**
   * Run the database seeds.
   */
  public function run()
  {
    Model::unguard();
    $this->call('PostTableSeeder');
    Model::reguard();
  }
}
class PostTableSeeder extends Seeder
{
  public function run()
  {
    App\Post::truncate();
    factory(App\Post::class, 1000)->create();
  }
}

這里有讀者會(huì)問(wèn):為什么我們不把填充操作都寫(xiě)在自帶的DatabaseSeeder的run函數(shù)里呢?

因?yàn)槲覀冮_(kāi)發(fā)一個(gè)完整的系統(tǒng)時(shí),可能要填充的數(shù)據(jù)表有很多張,不希望每次都要大量修改這個(gè)run函數(shù)。我們還希望每次填充都能保留下這個(gè)填充的過(guò)程,所以我們寧愿新寫(xiě)一個(gè)類(lèi),然后用$this->call()函數(shù)來(lái)調(diào)用。

接下來(lái)我們來(lái)談?wù)刦actory。

文件目錄\database\factories\ModelFactory.php

$factory->define(App\Post::class, function ($faker) {
  return [
    'title' => $faker->sentence(mt_rand(3, 10)),
    'content' => join("\n\n", $faker->paragraphs(mt_rand(3, 6))),
    'published_at' => $faker->dateTimeBetween('-1 month', '+3 days'),
  ];
});

雖然能看懂,但是不知道這個(gè)$factory變量是什么?因此去查Factory類(lèi)找。

在目錄\vendor\laravel\framework\src\Illuminate\Database\Eloquent的Factory.php找到源代碼

/**
* Define a class with a given set of attributes.
*
* @param string $class
* @param callable $attributes
* @param string $name
* @return void
*/
public function define($class, callable $attributes, $name = 'default')
{
    $this->definitions[$class][$name] = $attributes;
}

/**
* Create an instance of the given model and persist it to the database.
*
* @param string $class
* @param array $attributes
* @return mixed
*/
public function create($class, array $attributes = [])
{
    return $this->of($class)->create($attributes);
}

開(kāi)始填充數(shù)據(jù),我們還是使用artisan命令行

php artisan db:seed

這個(gè)命令會(huì)執(zhí)行你寫(xiě)在DatabaseSeeder.php里面所有的類(lèi)的run函數(shù),如果以后項(xiàng)目復(fù)雜了,沒(méi)有必要執(zhí)行已經(jīng)執(zhí)行過(guò)的,所以在命令行后面加參數(shù),只要執(zhí)行某個(gè)類(lèi)的run函數(shù)即可

php artisan db:seed --class=你要執(zhí)行的類(lèi)名稱(chēng)

更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Laravel框架入門(mén)與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》

希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • Laravel中數(shù)據(jù)遷移與數(shù)據(jù)填充的詳細(xì)步驟
  • Laravel實(shí)現(xiàn)數(shù)據(jù)庫(kù)遷移與支持中文的填充
  • laravel使用Faker數(shù)據(jù)填充的實(shí)現(xiàn)方法

標(biāo)簽:衡水 廣元 蚌埠 棗莊 大理 衢州 萍鄉(xiāng) 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel框架使用Seeder實(shí)現(xiàn)自動(dòng)填充數(shù)據(jù)功能》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266
    望城县| 临夏市| 林州市| 山西省| 化州市| 信丰县| 长葛市| 轮台县| 花莲市| 揭东县| 留坝县| 青神县| 临夏县| 涟水县| 潜江市| 集贤县| 镇雄县| 阳朔县| 万宁市| 库伦旗| 武隆县| 五家渠市| 淳安县| 余干县| 宁德市| 汉寿县| 周宁县| 阳城县| 林口县| 英吉沙县| 酉阳| 黑山县| 出国| 丹凤县| 华容县| 枣庄市| 盖州市| 资源县| 云南省| 延边| 华蓥市|