ReflectionException - Class DatabaseSeeder does not exist , Laravel Seeder [duplicate] - php

This question already has answers here:
Laravel 5 - artisan seed [ReflectionException] Class SongsTableSeeder does not exist
(14 answers)
Closed 1 year ago.
I have database seeder clases in diferent folder. When i write db:seed the console shows this error:
[ReflectionException] Class DatabaseSeeder does not exist , Laravel Seeder
One class is this:
namespace Database\Seeds;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use TiposCompromisosTableSeeder;
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Eloquent::unguard();
$this->call('TiposCompromisosTableSeeder');
}
}
and my other class is
namespace Database\Seeds;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class TiposCompromisosTableSeeder extends Seeder{
public function run(){
DB::table('tipos')->insert(array(
'nombre' => 'prioridad',
'tabla' => 'compromisos',
'str1' => 'baja',
'int1' => 1
));
}
}
I've tried to use
composer dump-autoupload
but doesn't work.
As you can see, I have both clases in the same namespace.
Help please.

If you've recently upgraded your Laravel version, check your composer.json
Your "autoload" section should look something like the snippet below
NOTE: you may have to add the "database" entry under the "classmap"
"autoload": {
"classmap": [
"app/Library",
"app/Models",
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/Library/helpers.php"
]
},
You should then run composer dump-autoload and try php artisan db:seed

remove your namespace definition in two classes, and use "composer dump-autoload".
Then it will works fine.

You should add this line into composer.json file inside "psr-4":
"Database\\Seeders\\": "database/seeders/"
it means that should be something like:
"autoload": {
"psr-4": {
"App\\": "app/",
"Modules\\": "Modules/",
"Database\\Seeders\\": "database/seeders/"
}
},

Just put it all in the DatabaseSeeder.php file like this:
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Eloquent::unguard();
$this->call('TiposCompromisosTableSeeder');
}
}
class TiposCompromisosTableSeeder extends Seeder{
public function run(){
DB::table('tipos')->insert(array(
'nombre' => 'prioridad',
'tabla' => 'compromisos',
'str1' => 'baja',
'int1' => 1
));
}
}

There is a need it only one change, rename the folder seed to seeders.

Solved: by adding
namespace database\seeds;
and then running command:
composer dump-autoload --no-dev

Also make sure database folder exist inside your project. If you accidently delete the database folder and run migration command. You will get this error too.

Does adding the --no-dev flag help?
composer dump-autoload --no-dev
source

Related

Laravel not finding the controller located in other folder

I'm trying to implement a clean architecture in laravel, thus I'm moving my own code to a src folder.
My controller is located in src\notebook\infrastructure but when i call it from routes\web.php this way:
Route::get('/notebook', 'src\notebook\infrastructure\NotebooksController#show');
i got this error:
Illuminate\Contracts\Container\BindingResolutionException
Target class [src\notebook\infrastructure\NotebooksController] does not exist.
http://127.0.0.1:8000/notebook
i also changed the namespace value in the class RouteServiceProvider from:
protected $namespace = 'App\Http\Controllers';
to
protected $namespace = '';
This is my notebook controller class:
namespace src\notebook\infrastructure;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class NotebooksController extends Controller
{
public function show($id)
{
echo 'controller from infrastructure folder';
}
}
My laravel and php version in composer.json are:
"php": "^7.2.5",
"laravel/framework": "^7.24",
I feel like i'm missing something stupid but can't figure it out what.
Did you add src folder into the autoload?
In composer.json file, you must have something like this:
"autoload": {
"psr-4": {
"App\\": "app/",
"Src\\": "src/" // add this
},
"classmap": [
"database/seeds",
"database/factories"
]
},
After changing it run composer dump-autoload.
And also don't forget to follow psr-4 rules and use Studly case namespace and class names.
namespace Src\Notebook\Infrastructure;

Reflection Exception : Class ClassName doesn't exist

I am using Laravel 5.5. I have added a custom directory inside App folder in my workspace. So, the folder structure is:
Inside App\Bishwa\Transformers there are two PHP files:
Transformer.php
LessonTransformer.php
Those files look like follows:
Transformer.php
<?php
namespace Bishwa;
abstract class Transformer {
public function transformCollection(array $items){
return array_map([$this, 'transform'], $items);
}
public abstract function transform($item);
}
LessonTransformer.php
<?php
namespace Bishwa;
class LessonTransformer extends Transformer {
public function transform($lesson){
return [
'title' => $lesson['title'],
'body' => $lesson['body'],
'active' => (boolean)$lesson['some_bool']
];
}
}
Then Inside LessonsController.php I have the following:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use App\Lesson;
use Bishwa\LessonTransformer;
class LessonsController extends Controller
{
protected $lessonTransformer;
function __construct(LessonTransformer $lessonTransformer){
dd('ok');
}
While running action of the controller, It gave me an error message saying:
Reflection Exception: Class Bishwa\LessonTransformer does not exist
I have tried composer dump-autoload, restarting the server again but none of them helped. Am I doing wrong while Namespacing or What?
Change your namespace of the files in your custom directory to App\Bishwa.
Well thanks to Jerodev and Jack. Since, both of them were write I decided to write myself a combined solution to this problem.
1st Solution:
In case of custom namespaces and custom classes I have to include the path to classname in Composer.json file in the following portion:
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Bishwa/Transformers"
],
"psr-4": {
"App\\": "app/"
}
2nd Solution:
Changing NameSpace of my files to custom directory App\Bishwa .
Namespace of transformer.php and LessonTransformer.php now becomes:
namespace App\Bishwa\Transformers;
While using in LessonsController:
use App\Bishwa\Transformers\LessonTransformer;
Once again, big thanks to Jerodev and Jack. Its my silly mistake, that I couldn't figure that out.

How to use traits in Laravel models?

Hello i'm laravel beginner
I want to make trait and use it in my model but at run time i got error that the trait is not found
my trait :
namespace App;
use Illuminate\Support\Facades\Schema;
trait GeneralModel
{
public static function testStaticFunction()
{
dd('test');
}
}
my model :
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
use GeneralModel;
}
my controller
namespace App\Http\Controllers;
use App\Comment;
class SearchController extends Controller
{
public function find()
{
Comment::testStaticFunction();
}
}
error received
Trait 'App\GeneralModel' not found
In composer.json add this:
"autoload" : {
"classmap": [
"database/seeds",
"database/factories"
],
"files" : [
"app/GeneralModel.php" // <----------- ADD THIS
],
"psr-4" : {
"App\\": "app/"
}},
Then run composer dump-autoload
Please check the GeneralModel.php in app folder. And execute the below command in your project root path.
php artisan dump-autoload
You Have to Use
Composer dump-autoload
In Your command line
hope it's help

Calling additional seeders in OctoberCMS fails with class not found error

I have seeder classes that I confirmed they could run individually, and I'm trying to run them from another seeder class in the same namespace.
I'm getting an error saying that the class run from another seeder is not found.
I'm using OctboerCMS build 419.
Seeder Class calling subseeder
<?php namespace Cocci\Custom\Updates;
use Seeder;
class SeedTablesCocciPedale extends Seeder
{
public function run()
{
$this->call('Cocci\Custom\Updates\SeedTablesCocciBike005');
$this->call('Cocci\Custom\Updates\SeedTablesCocciColor');
// $this->call(SeedTablesCocciBike005::class);
// $this->call(SeedTablesCocciColors::class);
}
}
Seeder called by another seeder
<?php namespace Cocci\Custom\Updates;
use Seeder;
use Cocci\Custom\Models\Product;
use Cocci\Custom\Models\Part;
use Cocci\Custom\Models\PreviewType;
use Cocci\Custom\Models\PartType;
use Cocci\Custom\Models\PartPreviewPosition;
class SeedTablesCocciBike005 extends Seeder
{
public function run()
{
$partTypeSetAll = PartType::create([
'name' => 'set_all',
'category' => PartType::CAT_ALL_PARTS,
]);
$partTypeNoPaintParts = PartType::create([
'name' => 'no_paint_parts',
'category' => PartType::CAT_NON_CUSTOMIZABLE,
]);
...
}
}
Error
% php artisan plugin:refresh Cocci.Custom
Rolled back: Cocci.Custom
Reinstalling plugin...
PHP Fatal error: Class 'Cocci\Custom\Updates\SeedTablesCocciBike005' not found in /Library/WebServer/Documents/cocci-custom/vendor/laravel/framework/src/Illuminate/Database/Seeder.php on line 62
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'Cocci\Custom\Updates\SeedTablesCocciBike005' not found
I tried both ways specifying the class by string of fully qualified name and with class keyword, but neither worked. Probably these seeder classes are not loaded?
What should I do?
Thanks in advance.
When using a seeder class to call additional seeder classes, you need to make sure you're autoloading the seeder classes.
Assuming you're seeder classes are called seed_tables_cocci_bike_005.php and seed_tables_cocci_color.php, you can add these files to you're classmap dev autoloader as follows.
"autoload-dev": {
"classmap": [
"tests/TestCase.php",
"tests/UiTestCase.php",
"tests/PluginTestCase.php",
"plugins/cocci/custom/updates/seed_tables_cocci_bike_005.php",
"plugins/cocci/custom/updates/seed_tables_cocci_color.php"
]
}
Remember to composer dumpautoload once your composer.json file has been updated.

How to keep seeder files separate and clean in laravel?

I want to keep my seeder files separate. for example UsersTableSeeder.php , PostsTableSeeder.php and then call them in main seeder file (DatabaseSeeder.php) :
Example:
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(UsersTableSeeder::class);
$this->call(PostsTableSeeder::class);
}
}
usersTableSeeder.php :
<?php namespace App\Seeds;
use Illuminate\Database\Seeder;
use App\User;
class UserTableSeeder extends Seeder {
public function run()
{
//DB::table('users')->delete();
// user1
User::create(array(
'name' => 'ahmad',
'email' => 'ahmad#ahmad.com',
'password' => 'ahmad'
));
}
}
my UsersTableSeeder.php and PostsTableSeeder.php files are in the same
directory that DatabaseSeeder.php is.
should I use psr-4 autoloading ? how?
Composer.json configuration
composer.json has a autoload key to configure additional autoload paths.
You can give it a try:
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
}
Example from my composer.json
Remove the namespace line from your seeder classes. They are found by the classmap entries now.
Explicit namespaces
OR
Keep the namespaces and add them in calls to ->call().
Wherever you write a classname you can fully qualify the class with its namespace (should be \database\seeds\YourClass::class, which can depend on your composer.json settings), or add a use statement at the beginning of the file.
You may have to run composer dump-autoload. It worked for me.
As a side note, it'd be cleaner to use PSR-4 for seeds and migrations (tests already do).

Categories