laravel seed生成測試資料

人生起落尋常事,花開花謝皆是春發表於2021-07-15
  1. 建立seed

    php artisan make:seeder OrderSeeder
  2. 執行composer

    composer dump-autoload 
  3. 編輯seed中run方法

    public function run()
    {
        DB::table("order")->truncate(); //截斷表
        // 設定為中文
        $faker = \Faker\Factory::create(config('app.faker_locale',"zn_CN"));
        //迴圈插入資料
        foreach (range(1,50) as $index){
        DB::table("order")->insert([
            "user_id" => rand(1,50),
            "order_sn" => 'on' . sprintf("%08d", $index),
            "order_status" => 1,
            "aftersale_status" => 0,
            "consignee" => $faker->name,
            "mobile" => $faker->phoneNumber,
            "message" => $faker->text,
            "goods_price" => rand(100,999),
            "freight_price" => rand(10,20),
            "coupon_price" => rand(1,9),
            "integral_price" => rand(1000,9999),
            "groupon_price" => rand(100,500),
            "order_price" => rand(100,999),
            "actual_price" => rand(100,999),
            "pay_id" => rand(1,100),
            "pay_time" => $faker->dateTime,
            "ship_sn" => 'sn' . sprintf("%08d", $index),
            "ship_channel" => "申通",
            "ship_time" => $faker->dateTime,
            "confirm_time" => $faker->dateTime,
            "end_time" => $faker->dateTime,
            "address" => $faker->address
        ]);
    }}

4.執行seed

php artisan db:seed
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章