---
description: https://laravel.com/docs/9.x/seeding
---

# Create Custom Factory (ok)

Tổng hợp nguồn để lấy trường facker \
[https://app.gitbook.com/o/-LVfVRgJVnpA6bttpEFT/s/-MCBeDUD-PK\_RF-YgJRe/tong-hop-nhung-truong-du-lieu-de-facker-ok](tong-hop-nhung-truong-du-lieu-de-facker-seeder-migration-migrate-factory-full-ok)

**Cách 1**:

```
php artisan make:seeder UserSeeder
```

C:\xampp\htdocs\api\database\seeders\UserSeeder.php

```
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class UserSeeder extends Seeder {
  /**
   * Run the database seeds.
   *
   * @return void
   */
  public function run() {
    DB::table('users')->insert([
      'name'     => Str::random(10),
      'email'    => Str::random(10) . '@gmail.com',
      'password' => Hash::make('password'),
    ]);
  }
}
```

Run

```
php artisan db:seed --class=UserSeeder
```

Kết quả:&#x20;

![](https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2Fs9Iigjgr7SsvEDYkU5ch%2FScreenshot_1.png?alt=media\&token=fd78391d-4b2d-47a8-9931-db14f78ebfaa)

_**Chú ý:**_ Theo một ví dụ mặc định&#x20;

C:\xampp\htdocs\api\database\seeders\DatabaseSeeder.php

```
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder {
  /**
   * Seed the application's database.
   *
   * @return void
   */
  public function run() {
    \App\Models\User::factory(10)->create();
  }
}
```

Chỉ cần chay dòng lệnh&#x20;

```
php artisan db:seed
```

**Cách 2**: đ

{% embed url="https://www.itsolutionstuff.com/post/how-to-use-factory-in-seeder-laravelexample.html" %}

```
php artisan make:migration item --create=Item
```

C:\xampp\htdocs\api\database\migrations\2022\_04\_24\_190230\_item.php

```
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Item extends Migration {
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up() {
    Schema::create('Item', function (Blueprint $table) {
      $table->bigIncrements('id');
      $table->string('title');
      $table->string('body');
      $table->timestamps();
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down() {
    Schema::dropIfExists('Item');
  }
}
```

```
php artisan make:model Item
```

C:\xampp\htdocs\api\app\Models\Item.php

```
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Item extends Model {
  use HasFactory;
  public $timestamps    = false;
  protected $table      = 'item';
  protected $primaryKey = 'id';
  protected $fillable   = ['title', 'body'];
}
```

```
php artisan make:seeder ItemSeeder
```

C:\xampp\htdocs\api\database\seeders\ItemSeeder.php

```
<?php
namespace Database\Seeders;
use App\Models\Item;
use Illuminate\Database\Seeder;
class ItemSeeder extends Seeder {
  /**
   * Run the database seeds.
   *
   * @return void
   */
  public function run() {
    Item::factory()->count(5)->create();
  }
}
```

```
php artisan make:factory ItemFactory --model=Item
```

C:\xampp\htdocs\api\database\factories\ItemFactory.php

```
<?php
namespace Database\Factories;
use App\Models\Item;
use Illuminate\Database\Eloquent\Factories\Factory;
class ItemFactory extends Factory {
  /**
   * The name of the factory's corresponding model.
   *
   * @var string
   */
  protected $model = Item::class;
  /**
   * Define the model's default state.
   *
   * @return array
   */
  public function definition() {
    return [
      'title' => $this->faker->title,
      'body'  => $this->faker->text,
    ];
  }
}
```

Sau cùng chạy:

```
php artisan db:seed --class=ItemSeeder
```

Kết quả thật vi diệu :)

![https://www.itsolutionstuff.com/post/how-to-use-factory-in-seeder-laravelexample.html](https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2FnAbfnIOe0XsIU2Ze2LLd%2FScreenshot_2.png?alt=media\&token=abfc0ea0-e956-4ff3-a470-339c333ce313)
