我的数据库中有两个表:
我需要一些虚拟数据,所以我创建了以下 SeedDummyOrders 类:
<?php
class SeedDummyOrders {
public function run()
{
DB::table('order_details')->delete();
DB::table('order_header')->delete();
$inventory_ids = range(1, 10);
for ($i = 1; $i <= 5; $i++)
{
$order = new \OrderHeader;
$order->owner_id = 1;
$order->order_number = '100' . $i;
$order->delivery_type = 'Standard';
$order->order_status = 'Received';
$order->address_line1 = $i . ', Some Street';
$order->city = 'London';
$order->postcode = 'ABC D' . $i;
$order->country_code = 'GB';
$order->country_name = 'Great Britain';
$order->contact_name = 'Mr Person ' . $i;
$order->contact_email = 'contact' . $i . '@mail.com';
$order->save();
$rand_keys = array_rand($inventory_ids, mt_rand(1, 5));
$detail_inventory_ids = [];
foreach ($rand_keys as $k)
$detail_inventory_ids[] = $inventory_ids[$k];
foreach ($detail_inventory_ids as $detail_inventory_id)
{
$detail = new \OrderDetail;
$detail->order_id = $order->id;
$detail->inventory_id = $detail_inventory_id;
$detail->qty_ordered = mt_rand(1, 10);
$detail->qty_picked = 0;
$detail->qty_packed = 0;
$detail->qty_dispatched = 0;
$detail->save();
}
}
}
}
我像这样运行播种机: php artisan db:seed --class = SeedDummyOrders
当我这样做时,我会收到以下错误:
{&#34;错误&#34; {&#34;类型&#34;:&#34; Symfony的\元器件\调试\异常\ FatalErrorException&#34;&#34;消息&#34;:& #34;调用 未定义的方法 SeedDummyOrders :: setContainer()&#34;&#34;文件&#34;:&#34; C:\瓦帕\ WWW \ MY_PROJECT \厂商\ laravel \框架\ SRC \照亮\数据库\控制台\ SeedCommand.php& #34;&#34;线&#34; 69}}
任何想法是什么,你如何解决它?
答案 0 :(得分:6)
好吧,我有一个愚蠢的错误。我忘了用 DatabaseSeeder 扩展这个类。
现在可行。
答案 1 :(得分:2)
我有同样的问题。运行composer dumpautoload
后,我收到以下消息:
vagrant@vagrant:/vagrant/getrileynow$ composer dumpautoload
Generating autoload files
Warning: Ambiguous class resolution, "GenerateEmailFilters" was found in both "/vagrant/project/database/migrations/2017_02_26_033525_generate_email_filters.php" and "/vagrant/project/database/seeds/GenerateEmailFilters.php", the first will be used.
将播种器文件重命名为其他内容后,我能够运行迁移。