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
Related
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
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';
Created the following file:
File: App\Services\Custom\Auth\AuthService.php
Name space: App\Services\Custom\Auth
Class name: AuthCustom
Method inside: foo()
In my controller I'm trying to call the foo method from the Service I created.
App\Services\Custom\Auth\AuthService\AuthCustom::foo()
Why does it keep returning Class 'App\Services\Custom\Auth\Authservice\AuthCustom' not found
What am I doing wrong?
Thanks!!
EDIT:
I added this in the composer.json and run composer dump-autoload without errors.
And it works!
"autoload": {
"classmap": [
"database",
"app/Services/Custom/Auth/AuthService.php"
],
"psr-4": {
"App\\": "app/"
}
},
Your namespace does not match your directory structure. If your class is in App\Services\Custom\Auth\AuthService.php, then your namespace needs to be App\Services\Custom\Auth. If you really want your namespace to be App\Custom\Auth, then your file needs to be App\Custom\Auth\AuthService.php.
Once you fix this, make sure you do a composer dump-autoload on the command line.
It seems that you didn't run composer dump-autoload or php composer.phar dump-autoload.
The composer.json is very important for autoloading!
Laravel needs an big file with all your php files required, usually generated via calling either artisan or composer with : php artisan dump-autoload / composer dump-autoload
It just regenerates the list of all classes that need to be included in the project (autoload_classmap.php). Ideal for when you have a new class inside your project.
More details: http://developed.be/2014/08/29/composer-dump-autoload-laravel/
I follow the docs: http://laravel.com/docs/master/migrations#database-seeding
I placed UserTableSeeder file near DatabaseSeeder. In resources/database/seeds/ folder.
These files are without namespaces (only classes in app/ are namespaced).
Of course there is an exception: exception 'ReflectionException' with message 'Class UserTableSeeder does not exist'
What is the best way to solve this problem?
The default Laravel 5 project has a classmap defined in its composer.json:
{
// ...
"autoload": {
"classmap": [
"database"
],
// ...
}
}
Run composer dump every time you add or remove a class on your database directory to update the Composer autoloader
Reference: https://github.com/laravel/laravel/blob/develop/composer.json
You should use composer dump-autoload command. From the docs:
Once you have written your seeder, you may need to regenerate Composer's autoloader using the dump-autoload command:
composer dump-autoload
Reference here.
I am trying to package my extending controller into my package. So, I put all my controllers in 'controllers' under 'src' folder.
MyController.php
namespace MyVendor\MyPackage;
use \Illuminate\Routing\Controller;
class MyController extends Controller
{
public function loginAction()
{
}
}
I tried to call it from route in package with MyVendor\MyPackage\MyController#loginAction and it end up with the message "Class MyVendor\MyPackage\MyController does not exist".
What did I missed or done wrong? How to make it works?
Thank you.
Step 1: make sure it is being autoloaded via composer. In composer.json (in your workbench/package):
"autoload": {
// ...
"classmap": [
"src/controllers",
],
// ...
},
Then run composer dump-autoload from the command line, but make sure you are in the package directory (e.g. workbench/name/package/)!
Step 2: add an alias in /app/config/app.php.