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' ]); } }
Writing SeedersA seeder class only contains one method by default: run . This method is called when the db:seed Artisan command is executed. Within the run method, you may insert data into your database however you wish. You may use the query builder to manually insert data or you may use Eloquent model factories.
Use Laravel seeder to insert data to database in laravel 8 If you wish to run seed on one table at a time, example if you want to run only UserSeeder. Just execute php artisan db:seed --class=UserSeeder. Now check database to see user inserted.
Laravel Seeding Creating a Seeder To create seeders, you may use the make:seeder Artisan command. All seeders generated will be placed in the database/seeds directory. Generated seeders will contain one method: run . You may insert data into your database in this method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With