php artisan db:seed,Laravel php artisan db:seed導致“使用”語句錯誤

When I try to run php artisan db:seed I get the following error:

當我嘗試運行php artisan db:seed時出現以下錯誤:

The use statement with non-compound name 'DB' has no effect

具有非復合名稱“DB”的use語句無效

I have written my own seeder file which I have included below, based on a snippet from the doc. As you can see I am using the use DB shortcut - is this what the problem is?

我根據文檔的片段編寫了我自己的播種文件,我在下面列出了該文件。正如您所看到的,我正在使用使用DB快捷方式 - 這是問題所在嗎?

use Illuminate\Database\Seeder;

use Illuminate\Database\Eloquent\Model;

use DB;

class ClassesTableSeeder extends Seeder

{

/**

* Run the database seeds.

*

* @return void

*/

public function run()

{

DB::table('classes')->delete();

DB::table('classes')->insert([

'class_name' => 'Test course 111',

'class_id' => '1',

'location_name' => 'Barnes',

'location_id' => '1',

'date' => '2015-06-22',

'month' => '06/2015',

'start_time' => '08:00',

'end_time' => '16:00',

'places' => '19',

'places_left' => '19',

'price' => '155.00'

]);

}

}

2 个解决方案

#1

40

In PHP the use statement is more of an alias than import. So since the ClassesTableSeeder class isn't in a defined namespace, you don't need to import the DB class. As a result you can remove use DB entirely.

在PHP中,use語句更多的是別名而不是import。因此,由於ClassesTableSeeder類不在已定義的命名空間中,因此您無需導入DB類。因此,您可以完全刪除使用DB。

#2

1

In seeder class You don't need to use DB statement on top of the page. Any alias written inside config>app.php aliases array don't require use statement. This is because seeder doesn't have any namespace.

在播種器類中您不需要在頁面頂部使用DB語句。在config> app.php別名數組中編寫的任何別名都不需要use語句。這是因為seeder沒有任何命名空間。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值