Laravel 5.1 Database Seeder ReflectionException Class does not exist - php

I am currently trying to use a Seeder class file, called in the DatabaseSeeder::run method to seed the database using the latest version of Laravel (Laravel 5.1). However when I run the db:seed artisan method I get the following error and no new items are added to the database:
[ReflectionException]
Class PostsSeeder does not exist
The source of the DatabaseSeeder.php file is as follows:
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(PostsSeeder::class);
Model::reguard();
}
}
And the source of the PostsSeeder.php file is:
use Illuminate\Database\Seeder;
class PostsSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
factory(App\Post::class, 25)->create();
}
}

Related

router stopped working after installing seeder

I installed seeder, and now web.php isn't working. Meaning- I can comment things out, change things around, etc., but I'll still always receive the laravel welcome page.
I'm putting here my seeder code, maybe the problem is rooted there- but the code the way it is now was the only way I was able to get 'php artisan db:seed' to work.
UserSeeder:
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('users')->insert([
'name'=> 'Admin',
'email'=> 'admin#medisonmedia.com',
'password'=> bcrypt('Aa123456')
]);
}
}
DatabaseSeeder:
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeders.
*
* #return void
*/
// public function run()
// {
// $this->call(UserSeeder::class);
// }
public function run()
{
\App\Models\User::factory(1)->create();
}
}

Laravel seeder issue1

i use php artisan db:seed --class UsersSeeder in cmd and it gives me error.
here is my code.
can anyone help me out?
<?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
class UsersSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
//
DB::table('users')->insert([
'name'=>'Yasaman',
'email'=>'Yasaman#gmail.com',
'password'=>Hash::make('1234')
]);
}
}
this is the error:
https://www.mediafire.com/file/5an383p1j4sm5m1/image.png/file
You probably have created_at and updated_at columns on your User db Table. Just give them a value and see if it works.

Target class [Database\Seeders\MockNotification] does not exist in Laravel

i am using a seeder class to seed a table in the database
<?php
namespace Database\Seeders;
class MockNotification extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
//
Notification::factory()->times(2)->create();
}
}
i am calling this class in the DatabaseSeeder
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Database\Seeders\NotificationTypeSeeder;
use Database\Seeders\MockNotification;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
// $this->call(UserSeeder::class);
$this->call([NotificationTypeSeeder::class]);
$this->call([MockNotification::class]);
}
}
i am getting this error
Target class [Database\Seeders\MockNotification] does not exist.
while i already imported MockNotification classs in the DatabaseSeeder file
Your problem is really simple to solve, you have to have your MockNotification class in LARAVEL_ROOT_FOLDER/database/seeders and then add this to the top of your class namespace Database\Seeders;.
Your DatabaseSeeder class should also have namespace Database\Seeders;. It is needed for composer to do PSR-4 autoloading.
Your seeder should be like:
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class MockNotification extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Notification::factory()->times(2)->create(); // Add this use on top
}
}
Add the following line in the beginning of seeder file
namespace Database\Seeders;
and then execute following in terminal
composer dumpautoload
Add
namespace Database\Seeders;
To both files.
It is standard PSR-4.

Calling php artisan db:seed does not work without providing a class name

I have a seeder class in the database folder
class UsersTableSeeder extends Seeder
{
public function run()
{
$user = new User();
$user->name = 'Name';
$user->email = 'email#gmail.com';
$user->password = bcrypt('secret');
$user->save();
}
}
When I run php artisan db:seed nothing happens, the seeder is only called when I run php artisan db:seed --class=UsersTableSeeder
This means I have to call each seeder class separately, any idea why db:seed doesn't work by it self?
Take a look at database/seeds/DatabaseSeeder.php
You will need to add the calls to your other seeders in there.
Then db:seed will function as expected
Example:
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$this->call(UsersTableSeeder::class);
$this->call(SecondSeedClass::class);
$this->call(ThirdSeedClass::class);
}
}
You have to register the seed classes in DatabaseSeeder class in seeds folder.
All the classes in run method will be seed on php artisab db:seed command
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* #return void
*/
public function run() {
$this->call(UsersTableSeeder::class);
$this->call(AnotherSeeder::class);
}
}
You need to add it to the DatabaseSeeder class:
public function run()
{
$this->call(UsersTableSeeder::class);
}

Class 'User' not found

So I'm trying a basic php artisan db:seed after migrating my database but it keeps returning the title error in cmd -[Symfony\Component\Debug\Exception\FatalErrorException] Class 'User' not found
Things I Have Tried
php dump-autoload after updating the class
php dump-autoload before running the db:seed function
rolling back the migration and then re-running it
rolling back the migration and then re-running it with the --seed syntax
Change the namespace of the 'Users' File
Below is the migrations
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
I believe that everything here is correct, and now here is the user class.
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
}
And now lastly is the all important database seeder
<?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');
$this->call('UserTableSeeder');
Model::reguard();
}
}
class UserTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->delete();
User::create(['email' => 'John#doe.com']);
}
}
So that's it my full syntax, if any more files are required then please request them and I will update my question.
In your DatabaseSeeder in the root namespace you call the Class User. It therefor tries to load the class User. The definition of your class User is however in namespace App. You should therefor use either App\User in your DatabaseSeeder or add at the top of the file use App\User;
DatabaseSeeder
<?php
use App\User;
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');
$this->call('UserTableSeeder');
Model::reguard();
}
}
class UserTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->delete();
User::create(['email' => 'John#doe.com']);
}
}
Since Laravel 8+ the User class is now stored by default in the app/Models directory; so instead of using App\User use App\Models\User above.
Ps. by default Laravel ships with a User model, use that one. In case you removed that model you can use the fallback provided by Laravel:
use Illuminate\Foundation\Auth\User;
On a side note something I find very useful in order to debug artisan output. You should use the flag -vvv which adds extreme verbosity to the output messages including a complete stack trace.
php artisan migrate -vvv

Categories