Laravel php artisan db:seed 导致“use”语句错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31088292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:50:44  来源:igfitidea点击:

Laravel php artisan db:seed leads to "use" statement error

phplaravellaravel-5artisan

提问by V4n1ll4

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

当我尝试运行时php artisan db:seed,出现以下错误:

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

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

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 DBshortcut - is this what the problem is?

我已经根据doc 中的一个片段编写了自己的播种机文件,并将其包含在下面。如您所见,我正在使用use DB快捷方式 - 这是问题所在吗?

<?php

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

回答by user2479930

In PHP the usestatement is more of an aliasthan import. So since the ClassesTableSeederclass isn't in a defined namespace, you don't need to import the DB class. As a result you can remove use DBentirely.

在 PHP 中,use语句与其说是 import,不如说是一个别名。因此,由于ClassesTableSeeder类不在定义的命名空间中,因此您无需导入 DB 类。因此,您可以完全删除use DB

回答by polodev

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

在播种机类中,您不需要use DB在页面顶部声明。写在config>app.phpaliases 数组中的任何别名都不需要use声明。这是因为播种机没有任何命名空间。

回答by Vahid Alvandi

in laravel migration you dont need call DB ;

在 Laravel 迁移中,您不需要调用 DB ;

remove use DB;

消除 use DB;