欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2018年06月13日 08:44:10   作者:編程老頭  
這篇文章主要介紹了Laravel框架使用Seeder實現(xiàn)自動填充數(shù)據(jù)功能,結合實例形式分析了Laravel基于Seeder類實現(xiàn)自動填充數(shù)據(jù)的相關操作技巧與注意事項,需要的朋友可以參考下

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

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

Laravel自動填充數(shù)據(jù)使用的是Seeder類

<?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只有一個run函數(shù),里面寫你的自動填充步驟

大家會注意到這兩個函數(shù)

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

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

在目錄\vendor\laravel\framework\src\Illuminate\Database\Eloquent下的Model.php下定義了這兩個函數(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作者的注釋可以知道,是對數(shù)據(jù)填充限制的操作。

所以unguard在前,reguard在后,unguard負責解除自動填充操作限制,reguard負責恢復限制。

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

Model::truncate();

這個函數(shù)會清空這個模型所對應的數(shù)據(jù)表,所以請慎重使用。

<?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();
  }
}

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

因為我們開發(fā)一個完整的系統(tǒng)時,可能要填充的數(shù)據(jù)表有很多張,不希望每次都要大量修改這個run函數(shù)。我們還希望每次填充都能保留下這個填充的過程,所以我們寧愿新寫一個類,然后用$this->call()函數(shù)來調(diào)用。

接下來我們來談談factory。

文件目錄\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'),
  ];
});

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

在目錄\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);
}

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

php artisan db:seed

這個命令會執(zhí)行你寫在DatabaseSeeder.php里面所有的類的run函數(shù),如果以后項目復雜了,沒有必要執(zhí)行已經(jīng)執(zhí)行過的,所以在命令行后面加參數(shù),只要執(zhí)行某個類的run函數(shù)即可

php artisan db:seed --class=你要執(zhí)行的類名稱

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

希望本文所述對大家基于Laravel框架的PHP程序設計有所幫助。

相關文章

最新評論