Hi i am trying to seed my database with fake data but when I did php artisan db:seed it give me an error: syntax error unexpected 'class' (t_class) expecting function (t_function). Dont know why I am getting this error.
My Database seeder.php file
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
$this->call('UserTableSeeder');
}
class UserTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->delete();
User::create([
'name' => 'John Doe',
'email' => 'Foo@example.com',
]);
}
}
}
The class UserTableSeeder is inside your other class (DatabaseSeeder).
This is not allowed in PHP.
Here's the correct code.
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
$this->call('UserTableSeeder');
}
}
class UserTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->delete();
User::create([
'name' => 'John Doe',
'email' => 'Foo@example.com',
]);
}
}
@RomainLanz I create a new file named UserTableSeeder.php in seeds folder and the pasted your code
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\User;
class UserTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->delete();
User::create([
'name' => 'John Doe',
'email' => 'Foo@example.com',
]);
}
}
Then I ran db:seed and nothing happend in localhost/phpmyadmin users table. Correct me if I am wrong db:seed will add fake data in the users table right ?