Laravel 7 - artisan seed Target class [DatabaseSeeder] does not exist - php

I'm trying to seed my "DatabaseSeeder.php' But when I try to run
php artisan db:seed
I get the error:
Target class [DatabaseSeeder] does not exist.
My code in "DatabaseSeeder.php" looks like this:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
factory(App\User::class, 3)->create()->each(function($u) {
$u->questions()
->saveMany(
factory(App\Question::class, rand(1, 5))->make()
);
});
}
}
What I tried so far:
composer dump-autoload
php artisan cache:clear
php artisan optimize
Note: I don't have a User-Made seeder, I'm trying to use the default one (DatabaseSeeder.php), but for some reason, it's telling me that it doesn't exist.

With Laravel 7, the composer.json needs the classmap reference in the autoload mapping, because the default seed folder doesn't respect PSR-4: database/seeds (see Laravel Documentation - Database: Seeding)
Check to have this in the composer.json:
"classmap": [
"database/seeds",
"database/factories"
],
And run composer dump-autoload
See this similar error: Target class [DatabaseSeeder] does not exist - Laravel 7 and Composer 2

Related

Cannot resolve this error: "Target class [DatabaseSeeder] does not exist."

Many people have asked about this error: Target class [DatabaseSeeder] does not exist. It seems to have many root causes, but I cannot determine my root cause.
I am using Laravel 6.20.43. The software does not produce any errors when run in the browser.
The error
The error appears when I use this command: php artisan db:seed
Here is DatabaseSeeder.php:
<?php
namespace Database\seeds;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use App\User;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
// factory$this->call(UsersTableSeeder::class);
// factory(App\User::class, 10)->create();
// dd('DatabaseSeeder.php TESTING...');
}
}
What have I tried?
I have tried to add a dd(...) inside DatabaseSeeder::run(). The dd(...) is not executed.
I have tried composer update. The update was performed nicely, but did not resolve the error.
I have tried several use clauses in DatabaseSeeder.php.
I have tried php artisan migrate:fresh
I have tried several combinations of solutions, for example to run migrations before dump-autoload and vice versa.
I have tried composer dump-autoload and this is the output:
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> #php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
Generated optimized autoload files containing 4381 classes
I have tried to redirect the output of php artisan db:seed to xdebug so I can analyse step by step what is happening. Good luck is what I needed here, but I ran out of luck.
I have tried to examine the error using php artisan db:seed -vvv. Here is the full output:
Illuminate\Contracts\Container\BindingResolutionException : Target class [DatabaseSeeder] does not exist.
at /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:805
801|
802| try {
803| $reflector = new ReflectionClass($concrete);
804| } catch (ReflectionException $e) {
> 805| throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
806| }
807|
808| // If the type is not instantiable, the developer is attempting to resolve
809| // an abstract type such as an Interface or Abstract Class and there is
Exception trace:
1 ReflectionException::("Class DatabaseSeeder does not exist")
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:803
2 ReflectionClass::__construct()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:803
3 Illuminate\Container\Container::build()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:681
4 Illuminate\Container\Container::resolve()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:785
5 Illuminate\Foundation\Application::resolve()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:629
6 Illuminate\Container\Container::make()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:770
7 Illuminate\Foundation\Application::make()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php:76
8 Illuminate\Database\Console\Seeds\SeedCommand::getSeeder()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php:63
9 Illuminate\Database\Console\Seeds\SeedCommand::Illuminate\Database\Console\Seeds\{closure}()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php:129
10 Illuminate\Database\Eloquent\Model::unguarded()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php:64
11 Illuminate\Database\Console\Seeds\SeedCommand::handle()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
12 Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Util.php:37
13 Illuminate\Container\Util::unwrapIfClosure()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
14 Illuminate\Container\BoundMethod::callBoundMethod()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
15 Illuminate\Container\BoundMethod::call()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:590
16 Illuminate\Container\Container::call()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Console/Command.php:134
17 Illuminate\Console\Command::execute()
/home/billybob/laravel-cursus1/vendor/symfony/console/Command/Command.php:255
18 Symfony\Component\Console\Command\Command::run()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Console/Command.php:121
19 Illuminate\Console\Command::run()
/home/billybob/laravel-cursus1/vendor/symfony/console/Application.php:1009
20 Symfony\Component\Console\Application::doRunCommand()
/home/billybob/laravel-cursus1/vendor/symfony/console/Application.php:273
21 Symfony\Component\Console\Application::doRun()
/home/billybob/laravel-cursus1/vendor/symfony/console/Application.php:149
22 Symfony\Component\Console\Application::run()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Console/Application.php:93
23 Illuminate\Console\Application::run()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:131
24 Illuminate\Foundation\Console\Kernel::handle()
/home/billybob/laravel-cursus1/artisan:37
I truly cannot understand how to debug the output of php artisan db:seed -vvv. All those files reside in the vendor directory, meaning I cannot examine the program flow easily. Please also explain how I can debug such an error by myself in the future.
It cannot find your DatabaseSeeder class because your namespace is incorrect.
You need to change the namespace of your DatabaseSeeder class from:
namespace Database\seeds;
to:
namespace Database\Seeders;
Make sure your composer autoload is setup correctly:
"autoload": {
"psr-4": {
"Database\\Seeders\\": "database/seeders/"
}
}
Alternatively, completely remove all namespaces from your DatabaseSeeder class and revert the above changes to composer.json and then run:
composer dump-autoload
then try running:
php artisan migrate:fresh --seed

Lumen 5.6 - php artisan db:seed got error 'Class DatabaseSeeder does not exist'

I'm trying to seed with Lumen 5.6.3 and executed the command:
php artisan db:seed.
Then I got error, saying
In Container.php line 767:
Class DatabaseSeeder does not exist
In my database/seeds directory, DatabaseSeeder.php does exist.
I've just copied the source in Lumen's official document and the source is like below.
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
//
}
}
I've googled many times to solve this error and of course tried composer dump-autoload, composer dump-autoload -o, composer dump-autoload --no-dev several times and the situation has never changed.
I also checked my composer/autoload_classmap.php and there is 'DatabaseSeeder' => $baseDir . '/database/seeds/DatabaseSeeder.php' so I looks like autoload does work correctly.
I really appreciate any advices or comments.
Thank you.
To fix this issue, you have to tweak your composer.json in order for
php artisan db:seed
to work
By default, Lumen has placed the database directory under autoload-dev.
"autoload-dev": {
"classmap": [
"tests/",
"database/"
]
},
To solve this, simple put the classmap together with your database directory under autoload
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/"
]
},
after tweaking run composer update command in order for the tweak to work.
You can use php artisan db:seed with lumen.
The command is: php artisan make:seeder Seedername.
For example you can use php artisan make:seeder UsersTableSeeder to create table seeder for the user.
The file will be created in the folder database\seeds.
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
factory(App\User::class, 10)->create();
}
}
This will create 10 example for the user class.
Then you should cinfigure the databaseseeder file
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Model::unguard();
// Register the user seeder
$this->call(UsersTableSeeder::class);
Model::reguard();
}
}
I set a wrong value for bootstrap/app.php.
I set like the below.
require_once __DIR__.'/../../vendor/autoload.php';
After I modified this part like following, I could run db:seed command correctly.
require_once __DIR__.'/../vendor/autoload.php';

PhpStorm - Some warnings on Laravel facades

I make right usage of Laravel's facades and PhpStorm gives me warnings, why is that?
And on image I pointed "x" for some...types of data? In functions I use, why do I have these? How to remove them?
Using facades with Laravel
Luke Waite is right:
You're not using facades. You've imported the classes, and on the
first, Categories, the IDE is telling you that the get method is not a
static method.
Just import the facade instead (if it exist).
See the documentation on Facades to learn more on how to use the available facades and how to define your own.
A facade class should look like this:
use Illuminate\Support\Facades\Facade;
class Cache extends Facade
{
/**
* Get the registered name of the component.
*
* #return string
*/
protected static function getFacadeAccessor()
{
return 'cache';
}
}
Where the 'cache' string is the name of a service container binding and defined in a service provider, something like this:
use App\Cache\MyCache;
use Illuminate\Support\ServiceProvider;
class CacheServiceProvider extends ServiceProvider
{
/**
* Register bindings in the container.
*
* #return void
*/
public function register()
{
$this->app->singleton('cache', function ($app) {
return new MyCache();
});
}
}
Fixing the warnings with Facades
That being said, I was tired of the warnings and the missing auto-completion and highlighting with facades so I also searched to find a way to fix these.
I came upon laravel-ide-helper which adds Laravel CLI commands that generates php files that only serves to be parsed by your IDE.
Install
Require this package with composer using the following command:
composer require barryvdh/laravel-ide-helper
After updating composer, add the service provider to the providers
array in config/app.php
Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class, To
install this package on only development systems, add the --dev flag
to your composer command:
composer require --dev barryvdh/laravel-ide-helper
In Laravel, instead of adding the service provider in the
config/app.php file, you can add the following code to your
app/Providers/AppServiceProvider.php file, within the register()
method:
public function register()
{
if ($this->app->environment() !== 'production') {
$this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
}
// ...
}
This will allow your application to load the Laravel IDE Helper on
non-production enviroments.
Automatic phpDoc generation for Laravel Facades
You can now re-generate the docs yourself (for future updates)
php artisan ide-helper:generate
Note: bootstrap/compiled.php has to be cleared first, so run php artisan clear-compiled before generating (and php artisan
optimize after).
You can configure your composer.json to do this after each commit:
"scripts":{
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan ide-helper:generate",
"php artisan ide-helper:meta",
"php artisan optimize"
]
},
The .phpstorm.meta.php and _ide_helper.php files will be generated and should be added to your .gitignore as you don't want to commit these.

Laravel 5: DB Seed class not found

I have this DatabaseSeeder.php:
<?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('MemberInvitationSeeder');
}
}
I have this file MemberInvitationSeeder.php, sibling to the DatabaseSeeder.php file
<?php
use Illuminate\Database\Seeder;
use App\MemberInvitation;
class MemberInvitationSeeder extends Seeder {
public function run()
{
MemberInvitation::truncate();
MemberInvitation::create( [
'id' => 'BlahBlah' ,//com_create_guid(),
'partner_id' => 1,
'fisrt_name' => 'Thats',
'last_name' => 'Me',
'email' => 'me#mymail.com',
'mobile_phone' => '444-342-4234',
'created_at' => new DateTime
] );
}
}
Now I call
php artisan db:seed
and I get:
[ReflectionException]
Class MemberInvitationSeeder does not exist
I tried everything I could find including "composer dump-autoload". to no avail. What am I doing wrong?
Step one - generate seed:
php artisan make:seed MemberInvitationSeeder
Step two - In DatabaseSeeder.php add line:
$this->call(MemberInvitationSeeder::class);
Step three:
composer dump-autoload
Step four:
php artisan db:seed
This should work
If this isn't the clue, check the composer.json file and make sure you have code below in the "autoload" section:
"classmap": [
"database"
],
I solved this by adding the class to the seeder file, with the instruction use:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\YourClassName;
I believe I know the reason now.
The new class MemberInvitationSeeder wasn't in the autoloaded classes in the composer.json file.
It wasn't there because I added that class manually.
Now, going forward, if I add such classes again, what should I use in order for my class to automatically to the autoloader?
This work for me
composer dump-autoload
php artisan db:seed
I ran into the similar error but I was using DB facade DB::table rather than model. I am posting the answer just in case somebody has similar issues.
The seeder file had namespace namespace Database\Seeders; and laravel was trying to look for DB class inside the namespace and hence the error appeared.
Class 'Database\Seeders\DB' not found.
Resolutions:
Remove the namespace and run composer dump-autoload
OR Add a backslash to \DB::table('stock_categories')->([ ... ]); so Laravel starts looking the DB facade from root (not from the namespace specified)
Eg,
\DB::table('MemberInvitation')->insert( [
'id' => 'BlahBlah' ,//com_create_guid(),
'partner_id' => 1,
'fisrt_name' => 'Thats',
'last_name' => 'Me',
'email' => 'me#mymail.com',
'mobile_phone' => '444-342-4234',
'created_at' => new DateTime
] );
If the above solutions doesn't work, try this one.
You may have changed the namespace (by default, it's "App").
What you need to do is to go to the composer.json file and check this:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
If the namespace is App like this example, this solution is not for you.
Otherwise, take the namespace you found and insert that line into your seeder class:
use NameSpaceFound\User;
You should include namespace if you want to use a string as a parameter
$this->call('Database\Seeders\MemberInvitationSeeder');
After making php artisan make:seed SeederName
then you need to run composer dump-autoload command after that
php artisan db:seed It is working for me.
I was facing this problem with laravel package with spatie
to solve this problem just
1- append seeders path in composer.json of your package
"autoload": {
"psr-4": {
...
"{packagename}\\{vendorname}\\Database\\Seeders\\": "database/seeders"
}
},
then go to your main project and update composer with composer update
auto-load your classess with composer dump-autoload
If it doesn't work delete Vendor folder and run composer install

Call database seeder from a subfolder

I want to create a set of database seed classes specifically for adding data for test cases I'm writing.
My plan was to put them in the folder:
app/database/seeds/testData/
and then call the seeder via the command:
php artisan db:seed --class="testData/myTestSeeder"
But I get a "class does not exist" error.
Is it possible to call database seeders that live in a subfolder in seeds? I don't see an explicit "yes" in the docs, but I don't see an explicit "no" either.
You shouldn't need to edit your classmap on your project, just make sure to run
composer dump-autoload
after moving your class to a subfolder.
Once you've done that, run this (no need to mention testData here)
php artisan db:seed --class="myTestSeeder"
You need to tell the autoloader how to load your new class. This is relatively simple; add the following to your composer.json in the classmap property:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/database/seeds/testData" // <-- Add this
]
},
After that, run composer dump-autoload and your seed file should now be loaded successfully.
When you create new Seeder class, for example:
php artisan make:seeder Authorization/CreateAppRoleSeeder
As a result, get new class:
<?php
use Illuminate\Database\Seeder;
class Authorization/CreateAppRoleSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
//
}
}
You have to replace class Authorization/CreateAppRoleSeeder extends Seeder
with class CreateAppRoleSeeder extends Seeder.
Next, execute the dump composer command:
composer dump
After add class to file 'database\Seeder\DatabaseSeeder'
In my case, for example:
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
$this->call(CreateAppRoleSeeder::class);
}
}
After that execute the command
php artisan db:seed
In my case
php artisan db:seed --class=CreateAppRoleSeeder
So will work ;)

Categories