Auto-populate data functionality with the Seeder of the Laravel framework

Source: Internet
Author: User
This article mainly introduces the Laravel framework uses the Seeder to realize the automatic filling data function, combined with the instance form analysis Laravel based on the Seeder class realization automatically fills the data the related operation skill and the attention matter, needs the friend can refer to the next

The examples in this article describe the Laravel framework using Seeder to automate the data-filling function. Share to everyone for your reference, as follows:

Laravel Auto-populated data is using the Seeder class

<?phpuse 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 ()  {    //  }}

Your custom Seeder has only one run function, which writes your autofill step

You'll notice that these two functions

Model::unguard ();//Your Fill operation Model::reguard ();

Once the two functions are very confused, what is the use of, can only be inferred is a pair of mutually counterproductive functions. Then went to check the source code.

These two functions are defined under model.php under directory \vendor\laravel\framework\src\illuminate\database\eloquent .

/*** 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 = f Alse;}

See the Laravel author's comments to know that it is an operation that limits the data population.

So Unguard in front, reguard after, Unguard is responsible for lifting the automatic filling operation limit, Reguard is responsible for restoring the limit.

It is recommended that you use the model's member functions before populating the operation

Model::truncate ();

This function clears the data table for this model, so use it carefully.

<?phpuse 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, +)->create ();  }}

Here are some readers who ask: why don't we write the fill operation in our own Databaseseeder run function?

Since we are developing a complete system, there are many sheets of data tables that may have to be populated, and we do not want to modify the run function much each time. We also want to keep the padding process for each fill, so we'd rather write a new class and then call it with the $this->call () function.

Next we'll talk about factory.

File directory \database\factories\modelfactory.php

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

Although can understand, but do not know this $factory variable is what? So go look for factory class.

Source code found in factory.php of directory \vendor\laravel\framework\src\illuminate\database\eloquent

/*** Define A class with a given set of attributes.** @param string $class * @param callable $attributes * @param string $na me* @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 $attribute s* @return mixed*/public function Create ($class, array $attributes = []) {    return $this->of ($class)->create ($ attributes);}

To start populating the data, we still use the artisan command line

PHP Artisan Db:seed

This command executes the run function of all the classes you write in the databaseseeder.php, and if the project is complicated later, there is no need to execute already, so add arguments after the command line, just execute the run function of a class.

PHP Artisan db:seed--class= The class name you want to execute

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.