I have a new Laravel Seeder but is giving a class does not exist error. The said seeder works in my local machine but not in production. The only file that I pushed was the seeder file itself.
File is in database\seeders
Filename: SeederName.php
Here is the code structure:
namespace Database\Seeders;
use App\Models\Model1;
use App\Models\Model2;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class SeederName extends Seeder
{}
When I run the command php artisan db:seed --class=SeederName, it returns a Target class [Database\Seeders\SeederName] does not exist.
One of the top answers given was to run the command composer dump-autoload However, when I run the said command, I am getting a Class Database\Seeders\SeederName located in ./database/seeders/SeederName.php does not comply with psr-4 autoloading standard. Skipping.
My composer.json
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
I also compared this new seeder with my other seeder file and code structure is the same.
I don't know why, but when I create a new seeder, now it works. Everything is the same except for the File Name and Class Name.
Illuminate\Contracts\Container\BindingResolutionException
Target class [Database\Seeders\CountriesTableSeeder] does not exist.
at C:\......\blog\vendor\laravel\framework\src\Illuminate\Container\Container.php:811
807▕
808▕ try {
809▕ $reflector = new ReflectionClass($concrete);
810▕ } catch (ReflectionException $e) {
➜ 811▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
812▕ }
813▕
814▕ // If the type is not instantiable, the developer is attempting to resolve
815▕ // an abstract type such as an Interface or Abstract Class and there is
1 C:\......\blog\vendor\laravel\framework\src\Illuminate\Container\Container.php:809
ReflectionException::("Class Database\Seeders\CountriesTableSeeder does not exist")
2 C:\......\blog\vendor\laravel\framework\src\Illuminate\Container\Container.php:809
ReflectionClass::__construct("Database\Seeders\CountriesTableSeeder")
From laravel 8 Seeders and factories are now namespaced
To accommodate for these changes, add Database\Seeders namespace to your seeder classes.
namespace Database\Seeders;
In addition, move all seeder files from previous database/seeds directory to database/seeders folder.
In your case remove all lines started with use Database\Seeders\...
from DatabaseSeeder.php file
It should solve the issue,
You can also run dump-autoload & fresh migration with seed,
composer dump-autoload
php artisan migrate:fresh --seed
For Laravel 8 you need to make the below changes to an existing project for seeding to work:
Add the Database\Seeders namespace to your DatabaseSeeder.php and other seeder files :
<?php
namespace Database\Seeders;
Change the folder name of database/seeds to database/seeders.
Update composer.json like below:
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
Finally, run the below commands:
composer dump-autoload
php artisan db:seed
remove using TableSeeder at the top of the DatabaseSeeder.php file.
When working with new seeders we might get
Target class [...TableSeeder] does not exist.
In my case, simply running
composer dump-autoload
did the trick
working with laravel 8
change directory name database/seeds to database/seeders.
add namespace Database\Seeders; to seeders file.
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class TableSeeder extends Seeder{
}
Update Compose.json file
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}},
run in terminal
composer dump-autoload
php artisan db:seed --class=TableSeeder
DatabaseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
$this->call([
CountriesTableSeeder::class,
ProvincesTableSeeder::class,
RegionsTableSeeder::class,
PermisionsTableSeeder::class,
RolesTableSeeder::class,
StatusTypesTableSeeder::class,
StatusesTableSeeder::class,
]);
}
}
CountriesTableSeeder.php
<?php
use App\Country;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class CountriesTableSeeder extends Seeder
{
private $numberOfCountries = 10;
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('countries')->insert([
['country_name' => 'iraq'],
['country_name' => 'qater'],
]);
}
}
After running: composer dump:autoload
I could just include the below code in my DatabaseSeeder and It would include the data from SubsidyregimeTableSeeder
$this->call([
SubsidyregimeTableSeeder::class
]);
In my case, I had an existing code of version 5.8 of Laravel.
So the files were duplicated in Seeds folder, the namespace was missing in and I had to reinstall Voyager with dummy like so :
composer dump-autoload
php voyager:install --with-dummy
fixed the namespace to namespace Database\Seeders; instead of namespace Database\Seeds;
Rename directory database/seeds to database/seeders
Open composer.json file and change on autoload section.
"autoload": {
"psr-4": {
"App\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
}
Open Database Seeder file database/seeders/DatabaseSeeder.php and add namespace Database\Seeders; on top of page.
Open your others seeders file and add namespace Database\Seeders; to all seeders class.
Run composer dump-autoload and php artisan migrate:fresh --seed
Remember if you upgrade ti laravel 8 you need to change directory name database/seeds to database/seeders.
Good Afternoon, I don´t understand this exception
ReflectionException in Container.php line 737: Class app\Http\Controllers\Login\LoginController does not exist
I know thats is related with the namespace but i configured my composer.json autoload. When i changed it to app\Http\Controllers\Login it works ok.
I also did artisan clear-compiled and composer dump-autoload
My Class is like this
<?php
namespace Login;
use Controller;
use Validator;
use Input;
use Auth;
use Redirect;
use View;
class LoginController extends Controller{}
My composer.json autoload
"autoload": {
"classmap":
[ "app/Http/Controllers" , "app/Models" , "database" ],
"psr-4": {
"app\\": "app/"
}
},
My Controllers Directory is like this
- app
-- Http
--- Controllers
---- Login
----- LoginController.php
-- Models
--- User
---- User.php
Thanks in advance
Edit: Why i don't have problems with my model files when i use a namespace like this:
namespace User;
In my config auth i have this:
'model' => User\User:class,
Your controller is in Login namespace, while it should be in app\Http\Controllers\Login namespace.
No entries in composer.json can change the way PHP's namespaces work - the mapping in there can just be used to tell autoloader where to look for physical files from given namespace.
I'm using PSR-4, I defined the psr-4 section on composer.json
"autoload": {
"classmap":[
],
"psr-4": {
"App\\": "app/"
}
},
When I tried to import a class by using "use" keyword
use App\Http\Controllers\Controller;
I get this error:
FatalErrorException in UploadFilesController.php line 13:
Class 'Controllers\Controller' not found
If I set the static route to the file
include("C:\xampp\...");
it works, but after that found the same problem with other files including PHP clases like Illuminate.
What's the problem?
in my Laravel app I have created Acme\fooDir directory inside app directory
i am trying to call a model -"barModel"- from within the fooDir
//app/Acme/fooDir/test.php
$barM = new barModel;
but i am getting this error
Class 'Acme\fooDir\barModel' not found
here is my app structure
app
-app/Acme
--app/Acme/fooDir
---app/Acme/fooDir/test.php (this is the file that could load the barModel)
-app/models
--app/model/barModel.php (this is the model i am trying to use)
I have added autoload in composer.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0":
{
"Acme": "app/"
}
and
I have ran the command
composer dump-autoload -o
and
php artisan dump-autoload
but the problem not solved and i am still getting the same error
Class 'Acme\fooDir\barModel' not found
any idea?
When using namespace all your classes are called within that namespace.
If you want to use barModel in Acme/fooDir/test.php your will need to use use barModel; just after your namespace row.
<?php
namespace Acme\fooDir;
use barModel;
class test {
}
Two things.
1, Have you namespaced your classes ie,
<?php namespace Acme\FooDir;
class Test ...
2, Are you using use in your class
<?php namespace Acme\FooDir;
use \BarModel
class Test ...
?
Would this do the trick for you.
$barM = new \barModel;
PHP namespaces and importing