💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# laravel 9.x 测试的 创建factory命令 > `php artisan make:factory 类名+Factory` 如下实例 类名和模型名称必须得一样 > `php artisan make:factory NewsFactory` *** 生成如下 ``` <?php namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Str; /** * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\News */ class NewsFactory extends Factory { /** * Define the model's default state. * * @return array<string, mixed> */ public function definition() { return [ // "title" =>Str::random(mt_rand(10,30)), "pid" =>mt_rand(1,10), "desc" =>Str::random(mt_rand(200,500)), ]; } } ``` 那这个NewsFactory怎么运行呢? > 我们先创建运行所有 factory 的一个seeder类,名称叫 RunfactorSeeder : 命令:`php artisan make:seeder RunfactorSeeder` 如下 ``` <?php namespace Database\Seeders; use App\Models\News; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; class RunfactorSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { News::factory(20)->create(); } } ``` 所有的 factory 可以这里运行 运行上面的 RunfactorSeeder ,就可以调用 NewsFactory 添加假数据 运行一个seeder。命令如下: > `php artisan db:seed --class=RunfactorSeeder`