当前位置:网站首页>Laravel8 fill data

Laravel8 fill data

2022-06-25 05:51:00 Yuan starts and ends

First , We need to download the latest version of laravel frame :

composer create-project --prefer-dist laravel/laravel www.zfw.com

After the download is completed, you can switch the directory to the... After the download is completed www.zfw.com Under the table of contents :

cd www.zfw.com

next , We can install a... In the frame PHPstorm Development plug-ins for , It can make our development more convenient and fast : 

composer require barryvdh/laravel-ide-helper

After downloading the plug-in , Using this command makes the editor we use better support plug-ins : 

php artisan ide-helper:gen

  If the above figure appears, the installation is successful .

Next , The environment we may use in development is a small skin system , We need to turn on ;

Then we need to configure a new local domain name for the newly generated framework , Be careful ! If the domain name of the newly downloaded framework is set by the Xiaopi system , The contents of the hidden entry file will disappear , Need to rewrite the entry file ! 

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>
 
    RewriteEngine On
 
    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
 
    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]
 
    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

or

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>

After setting the local domain name , We can enter the local domain name just set in the website , See if you can enter :

The framework is basically completed , Now let's do the filling configuration

here , We need to modify the database information that the framework needs to connect to in the framework's configuration file , The following configuration information needs to be written in the root directory of the framework env In file :

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=wwwzfwcom
DB_USERNAME=root
DB_PASSWORD=root
DB_PREFIX=zfw_

among database For the database name in the database ,username Account number for the database ,password Password for the database ,prefix Prefix the table with

Next , The database tool we use is Navicat, You need to create a new database :

  Then , We need to create a file in the root directory of the framework config Found a folder named database.php Find this method in the file and modify : 

'prefix' => env('DB_PREFIX'),

Then we can modify the local configuration of the framework , Because of the laravel The framework is built for foreign developers , So we need to change the localization configuration of the framework :

1: Change the root directory app.php in timezone The time zone of is :

'timezone' => 'PRC',

2: Download the Chinese language pack , Put in the frame root directory ->resources->lang in

use first composer download :

composer require "overtrue/laravel-lang:~3.0"

Second, replace config/app.config Class in

take :

Illuminate\Translation\TranslationServiceProvider::class,

Replace with :

Overtrue\LaravelLang\TranslationServiceProvider::class,

Last modified config/app.config Medium locale:

'locale' => 'zh-CN',

3: take Faker The filling class configuration is modified to :

'faker_locale' => 'zh_CN',

In this way, the basic configuration is completed , Let's start setting the migration data table , Create build models and migration files :

php artisan make:model User -m

next , In the frame of database/migrations The newly created user table... Is found in the folder user

  In a up Write the information to create the database in the method of :

            $table->bigIncrements('id');
            //  role 
            $table->unsignedInteger('role_id')->default(0)->comment(' role ID');
            $table->string('username',100)->comment(' account number ');
            $table->string('truename',50)->default(' Unknown ')->comment(' What a name ');
            $table->string('password',255)->comment(' password ');
            //nullable  It can be for null
            $table->string('email',50)->nullable()->comment(' mailbox ');
            $table->string('phone',15)->default('')->comment(' Phone number ');
            $table->enum('sex',[' sir ',' ma'am '])->default(' sir ')->comment(' Gender ');
            $table->char('last_ip',15)->default('')->comment(' Sign in IP');
            $table->timestamps();
            // Soft delete   Generate a field  deleted_at Field 
            $table->softDeletes();

Then perform the migration file at this time :

php artisan migrate

here , Such error messages may occur

   We need to be in app->Providers->AppServiceProvider Medium boot Use in

use Illuminate\Support\Facades\Schema;
 
 
    public function boot()
    {
        Schema::defaultStringLength(191);
    }

Then you can continue to execute the migration command

  Last , We started using Faker Fill in the data

Create user's fill file

php artisan make:seeder UserSeeder

here , We need to be in user Use... In the model layer

protected $guarded = [];

Otherwise , Will not be able to fill the data

next , We generate a data factory

php artisan make:factory UserFactory -m /User

stay UserFactory.php Modify the data filling the file in the file

            'username' => $this->faker->userName,
            'password' => bcrypt('admin'),
            'truename' => $this->faker->name,
            //password_hash('admin666')
            'email' => $this->faker->unique()->safeEmail,
            'phone' => $this->faker->unique()->phoneNumber,

stay userseeder.php In file :

        // Reset table data 
        User::truncate();
 
        User::factory()
         // Generate 100 Data 
         ->count(100)
        //->hasPosts(1)
         ->create();
        // modify id=1 user 
        User::where('id',1)->update(['username'=>'admin']);

Last in DatabaseSeeder.php Modify the total call

         $this->call([
            UserSeeder::class
        ]);

Execute the fill command

php artisan migrate:fresh --seed

Here we are , Filling complete

laravel8 Fill in the data _SunNang The blog of -CSDN Blog _laravel8 Data filling

原网站

版权声明
本文为[Yuan starts and ends]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202201255211475.html