I am trying to run a seeder in a lumen 8 project but have been unsuccessful for a while now. Here's the folder structure view
When I run php artisan db:seed --class="Database\Seeds\SampleTableSeeder" or php artisan db:seed --class="Database\Seeders\Seeds\SampleTableSeeder" or php artisan db:seed --class=SampleTableSeeder it fails and returns a target class not found like
Target class [Database/Seeds/SampleTableSeeder] does not exist.
I have added the namespace to SampleTableSeeder to look like
<?php
namespace Database\Seeds;
use App\Models\Sample;
class SampleTableSeeder
{
// ....
}
And in DatabaseSeeder run function, I am calling it like this
$this->call('Database/Seeds/SampleTableSeeder');
What is the right way to run this particular seeder?
Database/Seeds will never work, because you have not declare it on the composer.json section...
See the original file, it has this section:
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
}
You have to declare that new section:
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/",
"Database\\Seeds\\": "database/seeds/"
}
},
}
Then it will work, but I personally do NOT recommend to do that... you already have Database/Seeders...
Quick update, if running php artisan db:seed --class="Database\Seeds\SampleTableSeeder" does not work, try adding \ at the beginning, like this php artisan db:seed --class="\Database\Seeds\SampleTableSeeder"
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.
I'm trying to use symfony flex with a bundle.
I have this directory structure
/
src/
AppBundle/
AppBundle.php
# Many classes
Kernel.php
I want to load Kernel.php class with this namespace App and classes inside AppBundle with the namespace AppBundle.
I've tried many composer configurations but I couldn't load them.
"psr-4": {
"AppBundle\\": "src/AppBundle/",
"App\\": "src/"
}
But I got errors like this:
Expected to find class "App\AppBundle\AppBundle" in file "/var/www/vhosts/flex/src/AppBundle/AppBundle.php"
UPDATE
src/Kernel.php class have a different namespace and I couldn't change it because other classes used it, the namespace is App. Some scripts call that class using use App\Kernel
src/AppBundle/AppBundle.php class has this namespace AppBundle
There is way to do this?
UPDATE 2
I sorted it out:
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle/"
},
"classmap": [
{ "App\\Kernel": "src/Kernel.php" }
]
},
I would suggest using a classmap for Kernel.php instead:
"autoload": {
"psr-4": { "AppBundle\\": "src/AppBundle" },
"classmap": [ "src/Kernel.php" ]
}
See https://getcomposer.org/doc/04-schema.md#classmap
You only need
"psr-4": { "App\\": "src/" }
I'm trying to set up unit tests for my Laravel (5.2) API project. Before I use the unit tests I'd like to define a custom namespace for them so I created the namespace Test; in the default TestCase.php file. Like so:
namespace Test;
class TestCase extends Illuminate\Foundation\Testing\TestCase
{
...
}
Then I created a folder UnitTests under the tests folder and put my unit tests in that folder with the following namespace:
namespace Test\UnitTests;
use Test\TestCase;
class CreateAccountTest extends TestCase
{
...
}
Now when I want to run my unit tests I get the following error:
PHP Fatal error: Class 'Test\Illuminate\Foundation\Testing\TestCase' not found in /var/www/ops/tests/TestCase.php on line 6
So basically, Laravel thinks the Illuminate\Foundation\Testing\TestCaseclass is found within the Test namespace instead of the Illuminatenamespace from Laravel.
I also have the following autoload configured in the composer.json file:
"autoload": {
"classmap": [
"database",
"tests"
],
"psr-4": {
"App\\": "app/",
"Test\\": "tests/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
}
I've also tried running both commands with no success:
php artisan config:cache
composer dump-autoload
As #xmike mentioned in the comments of the question I didn't add a backslash to the namespace.
So instead of
class TestCase extends Illuminate\Foundation\Testing\TestCase
it should be
class TestCase extends \Illuminate\Foundation\Testing\TestCase
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?