方法 Illuminate\Database\Query\Builder::setContainer 不存在

Posted

技术标签:

【中文标题】方法 Illuminate\\Database\\Query\\Builder::setContainer 不存在【英文标题】:Method Illuminate\Database\Query\Builder::setContainer does not exist方法 Illuminate\Database\Query\Builder::setContainer 不存在 【发布时间】:2018-09-24 08:30:44 【问题描述】:

播种者:

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')
    ];
);

【问题讨论】:

【参考方案1】:

你不应该在 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);
  

【讨论】:

谢谢!错误本身不是自我解释的。问题解决了! 不客气 ;),是的,它本身并不能自我解释。

以上是关于方法 Illuminate\Database\Query\Builder::setContainer 不存在的主要内容,如果未能解决你的问题,请参考以下文章

init()方法和构造方法的区别

GroovyGroovy 扩展方法 ( 扩展静态方法示例 | 扩展实例方法示例 | 扩展实例方法与扩展静态方法代码相同 )

Python中静态方法和类方法的区别

类方法和实例方法

Java中的构造方法与普通方法的区别? 啥情况下用构造方法啥情况下用普通的方法

Python中静态方法和类方法的区别