播种机:
class CouponTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$coupons = (array)factory(\App\Coupon::class,10)->make();
Log::info('Created coupons.. ', $coupons);
}
}
class CompanyTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$companies = (array)factory(\App\Company::class,10)->make();
Log::info('Company users.. ', $companies);
}
}
class CustomerTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$customers = (array)factory(\App\Customer::class,10)->make();
Log::info('Created customers.. ', $customers);
}
}
class UserTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$users = (array)factory(User::class,10)->make();
Log::info('Created users.. ', $users);
}
}
class DBSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$classes = [\App\Company::class,\App\Customer::class,\App\Coupon::class,\App\Employee::class,\App\User::class];
$this->call($classes);
}
}
我跑php artisan db:seed --class=DBSeeder
后
播种后我收到错误:App \ Company:
BadMethodCallException:方法Illuminate \ Database \ Query \ Builder :: setContainer不存在。
整个痕迹:
异常追踪:
1 Illuminate \ Database \ Query \ Builder :: __ call(“setContainer”) /Applications/XAMPP/xamppfiles/htdocs/sites/FakeProject/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:1288
2 Illuminate \ Database \ Eloquent \ Builder :: __ call(“setContainer”) /Applications/XAMPP/xamppfiles/htdocs/sites/FakeProject/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1511
工厂:
$factory->define(Company::class, function (Faker $faker) {
return [
"company_id"=>$faker->randomNumber(3000),
"logo"=>$faker->unique()->company
];
});
$factory->define(Coupon::class, function (Faker $faker) {
$types = ['Food','Travelling','Camping','Sport','Restaurants','Health'];
return [
"coupon_id"=>$faker->unique()->randomNumber(2000),
"title"=>$faker->unique()->randomLetter,
'amount'=>rand(1,50),
'price'=>(double)rand(1,100),
'type'=>array_rand($types),
'created_at'=>\Carbon\Carbon::now(),
'expired_at'=>\Carbon\Carbon::create(rand(2018,2020),rand(1,12),rand(1,31))
];
});
$factory->define(Customer::class, function (Faker $faker) {
return [
'customer_id'=>$faker->unique()->randomNumber(1000),
'first_name'=>$faker->name,
'last_name'=>$faker->name,
'nickname'=>$faker->unique()->userName,
'email'=>$faker->unique()->safeEmail,
'password'=>$faker->unique()->password
];
});
$factory->define(Employee::class, function (Faker $faker) {
return [
'employee_id'=>$faker->unique()->randomNumber(1000),
'first_name'=>$faker->name,
'last_name'=>$faker->name,
'nickname'=>$faker->unique()->userName,
'email'=>$faker->unique()->safeEmail,
'password'=>$faker->unique()->password
];
});
$factory->define(App\User::class, function (Faker $faker) {
return [
'user_nickname' => $faker->unique()->name,
'email' => $faker->unique()->safeEmail,
'password' => crypt(rand(1,1000),'plmaks'),
'remember_token' => str_random(10),
'id'=>$faker->unique()->randomNumber(3000),
'created_at'=>Carbon::now()->format('Y-m-d H:i:s'),
'updated_at'=>Carbon::now()->format('Y-m-d H:i:s')
];
});
答案 0 :(得分:2)
您不应该在DBSeeder
中调用模型,请调用您的Seeder类:
class DBSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$classes =[UserTableSeeder::class,
CompanyTableSeeder::class,
CustomerTableSeeder::class,
CouponTableSeeder::class,
];
$this->call($classes);
}
}