User Tools

Site Tools


laravel

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
laravel [2020/12/23 18:57] chifeklaravel [2023/09/14 06:06] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Seeders Laravel ======+[[laravel-mix|Laravel Mix]]
  
 +[[laravel-mix-vuejs|Laravel + Laravel Mix + VueJS]]
  
-**Introduction**+[[Seeder|Seeder]]
  
 +[[Collections|Collections]]
  
-Laravel includes the ability to seed your database with test data using seed classes. All seed classes are stored in the `database/seeders` directory. By default, a `DatabaseSeeder` class is defined for you. From this class, you may use the `call` method to run other seed classes, allowing you to control the seeding order.+[[authSocial|Авторизация через соцсети]]
  
  
- +[[artisan_shpargalki|artisan shpargalki]]
-**Writing Seeders** +
- +
- +
-To generate a seeder, execute the `make:seeder` Artisan command. All seeders generated by the framework will be placed in the database/seeders directory: +
- +
-<code>php artisan make:seeder UserSeeder</code> +
- +
-A seeder class only contains one method by default: `run`. This method is called when the `db:seed` Artisan command is executed. Within the `run` method, you may insert data into your database however you wish. You may use the query builder to manually insert data or you may use Eloquent model factories. +
- +
-As an example, let's modify the default DatabaseSeeder class and add a database insert statement to the run method: +
- +
-<code> +
-<?php +
- +
-namespace Database\Seeders; +
- +
-use Illuminate\Database\Seeder; +
-use Illuminate\Support\Facades\DB; +
-use Illuminate\Support\Facades\Hash; +
-use Illuminate\Support\Str; +
- +
-class DatabaseSeeder extends Seeder +
-+
-    /** +
-     * Run the database seeders. +
-     * +
-     * @return void +
-     */ +
-    public function run() +
-    { +
-        DB::table('users')->insert([ +
-            'name' => Str::random(10), +
-            'email' => Str::random(10).'@gmail.com', +
-            'password' => Hash::make('password'), +
-        ]); +
-    } +
-+
-</code> +
- +
- +
-**Using Model Factories** +
- +
-Of course, manually specifying the attributes for each model seed is cumbersome. Instead, you can use model factories to conveniently generate large amounts of database records. First, review the model factory documentation to learn how to define your factories. +
- +
-For example, let's create 50 users that each have one related post: +
- +
- +
-<code> +
-use App\Models\User; +
- +
-/** +
- * Run the database seeders. +
- * +
- * @return void +
- */ +
-public function run() +
-+
-    User::factory() +
-            ->times(50) +
-            ->hasPosts(1) +
-            ->create(); +
-+
-</code>+
laravel.1608749862.txt.gz · Last modified: 2023/09/14 06:06 (external edit)