V4n1ll4
V4n1ll4

Reputation: 6099

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

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

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

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

Upvotes: 28

Views: 30908

Answers (5)

Abdul Manan
Abdul Manan

Reputation: 71

use the following instead of use DB

use Illuminate\Support\Facades\DB;

Upvotes: 7

Mehmet B&#252;t&#252;n
Mehmet B&#252;t&#252;n

Reputation: 749

Use the following instead of use DB.

use DB as DBS;

After that, you can use it as follows.

DBS::table('foo')->insert([
            'name'=>'bar',
        ]);

Upvotes: 2

Vahid Alvandi
Vahid Alvandi

Reputation: 612

in laravel migration you dont need call DB ;

remove use DB;

Upvotes: 2

polodev
polodev

Reputation: 319

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.

Upvotes: 2

Jocke Med Kniven
Jocke Med Kniven

Reputation: 4049

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.

Upvotes: 64

Related Questions