Illuminate\Contracts\Container\BindingResolutionException : Target class [Database\Seeders\PermissionTableSeeder] does not exist - php

So I have a problem with seeding, the first time I got this error I thought maybe the name of the file and the table is not the same. I tried looking it up and what I found was from this link in laravel 8 with seeding , i has this issue Target class [TableSeeder] does not exist and it says to edit the namespace from Database\Seeds to Database\Seeders but that's not the problem because it's already correct. It says to check the composer.json for autoload and change Seeds to Seeders...
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
but I also have the exact same thing as they suggested. And after that some people also suggested doing composer dump-autoload and this is the result....
Generating optimized autoload files
Class App\Http\Requests\UpdaUserRequest located in \app\Http\Requests\UpdateUserRequest.php does not comply with psr-4 autoloading standard. Skipping.
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> #php artisan package:discover --ansi
INFO Discovering packages.
jenssegers/agent ............................ DONE
laravel/fortify ............................. DONE
laravel/jetstream ........................... DONE
laravel/sail ................................ DONE
laravel/sanctum ............................. DONE
laravel/tinker .............................. DONE
livewire/livewire ........................... DONE
nesbot/carbon ............................... DONE
nunomaduro/collision ........................ DONE
nunomaduro/termwind ......................... DONE
spatie/laravel-ignition ..................... DONE
Generated optimized autoload files containing 5496 classes
And I tried to seed it again and it gave me this....
php artisan db:seed
INFO Seeding database.
Illuminate\Contracts\Container\BindingResolutionException
Target class [Database\Seeders\PermissionTableSeeder] does not exist.
at \vendor\laravel\framework\src\Illuminate\Container\Container.php:877
873▕
874▕ try {
875▕ $reflector = new ReflectionClass($concrete);
876▕ } catch (ReflectionException $e) {
If anyone can help me, I am very grateful for anyone that can help. This is my DatabaseSeeder.php...
<?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
$this->call([
PermissionTableSeeder::class,
RolesTableSeeder::class,
PermissionRoleTableSeeder::class,
UsersTableSeeder::class,
RoleUsersTableSeeder::class,
]);
}
}
and this is the PermissionsTableSeeder.php....
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class PermissionsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$permissions = [
[
'id' => 1,
'title' => 'user_access',
],
[
'id' => 2,
'title' => 'task_access',
],
];
Permissions::insert($permissions);
}
}

I got the answer, so first of all apparently the name for table seeder is not right. And then I got this error...
Error
Class "Database\Seeders\permissions" not found
at \database\seeders\PermissionsTableSeeder.php:28
24▕ 'title' => 'task_access',
25▕ ],
26▕ ];
27▕
➜ 28▕ permissions::insert($permissions);
29▕ }
30▕ }
31▕
and after I scour through the internet I found this https://laracasts.com/discuss/channels/laravel/laravel-8-class-databaseseedersdb-not-found. So I read the answer so you need to add use Illuminate\Support\Facades\DB; in your table seeder so it can call the database to get it to work. So now my PermissionsTableSeeder.php looks like this and it works just fine.
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class PermissionsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$permissions = [
[
'id' => 1,
'title' => 'user_access',
],
[
'id' => 2,
'title' => 'task_access',
],
];
DB::table('permissions')->insert($permissions);
}
}

Related

DB Seeder class not found

I am trying to seed a database with the following files, 'ItemsTableSeeder' and 'UsersTableSeeder', but I keep on receiving the message: "Target class [ItemsTableSeeder] does not exist." when I run php artisan db:seed.
The files were added in the command prompt with the commands 'php artisan make:seeder ItemsTableSeeder' and 'php artisan make:seeder UsersTableSeeder'.
Here's the structure of my files:
-database
-- factories
ItemFactory.php
UserFactory.php
-- seeders
DatabaseSeeder.php
ItemsTableSeeder.php
UsersTableSeeder.php
// ItemsTableSeeder.php
<?php
use Illuminate\Database\Seeder;
class ItemsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
factory(\App\Items::class, 20)->create();
}
}
// UsersTableSeeder.php
<?php
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
factory(\App\Users::class, 3)->create();
}
}
DatabaseSeeder.php
<?php
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('ItemsTableSeeder');
$this->call('UsersTableSeeder');
}
}
I have tried running 'composer dump-autoload' after adding the two seeder files, but at this stage this is not making any difference. I have also tried clearing the cache 'php artisan config:cache'.
Here is an extract from my 'autoload_classmap.php' (after running composer dump-autoload):
'Cron\\HoursField' => $vendorDir . '/dragonmantank/cron-expression/src/Cron/HoursField.php',
'Cron\\MinutesField' => $vendorDir . '/dragonmantank/cron-expression/src/Cron/MinutesField.php',
'Cron\\MonthField' => $vendorDir . '/dragonmantank/cron-expression/src/Cron/MonthField.php',
'Database\\Factories\\ItemFactory' => $baseDir . '/database/factories/ItemFactory.php',
'Database\\Factories\\UserFactory' => $baseDir . '/database/factories/UserFactory.php',
'DeepCopy\\DeepCopy' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/DeepCopy.php',
'DeepCopy\\Exception\\CloneException' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/Exception/CloneException.php',
'DeepCopy\\Exception\\PropertyException' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/Exception/PropertyException.php',
'DeepCopy\\Filter\\Doctrine\\DoctrineCollectionFilter' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/Filter/Doctrine/DoctrineCollectionFilter.php',
'DeepCopy\\Filter\\Doctrine\\DoctrineEmptyCollectionFilter' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/Filter/Doctrine/DoctrineEmptyCollectionFilter.php',
'DeepCopy\\Filter\\Doctrine\\DoctrineProxyFilter' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/Filter/Doctrine/DoctrineProxyFilter.php',
Any ideas of other things to try would be greatly appreciated,
Thanks,
Robert
London, England
add namespace Database\Seeders; at top of each seeder files
Then composer dump-autoload
Your structure files working good.
Try to fresh migrite
php artisan migrate:fresh --seed
if still not fixed try this: php artisan db:seed --force
then:
1- composer dump-autoload
2- php artisan db:seed
let me know if that work
Have you tried add namespace Database\Seeders; before run command?
Probably you forget to import Users model.
For example:
database\seeders\UsersSeeder.php
namespace Database\Seeders;
use App\Models\Users;
use Illuminate\Database\Seeder;

Laravel Factories how to call them from your database seeder file

How to call the factorie files from the database seeder I looked at the laravel docs and was trying around whit this code
https://laravel.com/docs/5.7/seeding
factory(App\User::class, 50)->create()->each(function ($user) {
$user->posts()->save(factory(App\Post::class)->make());
});
But it didnt work so mine question is how do i call mine factorie from mine database seeder file so i can execute them from the command line?
databaseseeder
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
Eloquent::unguard();
$this->call(UserFactory::class);
$this->call(PostFacory::class);
$this->call(ProfileFacory::class);
$this->command->info("Database seeded.");
Eloquent::reguard();
}
}
user factorie
<?php
use Faker\Generator as Faker;
$factory->define(App\User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => bcrypt('password'),
'remember_token' => str_random(10),
];
});
Database seeding is
php artisan db:seed
In the laravel path... also if you have already seeded you have to clean ( sure if do not have inportant data )
php artisan migrate:refresh
Carefully here :)

Laravel Seeding missing classes

I am following this tutorial until I need to generate seeds using: php artisan db:seed. It always said that my Article and User class are not found.
I have looking for solution like in:
https://laracasts.com/discuss/channels/lumen/unable-to-run-php-artisan-dbseed-due-to-missing-class (setting up composer.json's auto load paths and composer dump-autoload)
Laravel cannot find class of a package
I have deleting my vendor folder and do composer install again
Also importing the file manually, require_once with relative path to the model from the seeding or root of the projet, but neither works.
I think this should work out-of-the-box but it isn't. What is my problem? And what is my solution?
EDIT 1: Someone requested seeders codes here you are!
Article Seeder
<?php
use Illuminate\Database\Seeder;
class ArticlesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
// Let's truncate our existing records to start from scratch.
Article::truncate();
$faker = \Faker\Factory::create();
// And now, let us create a few articles in our database:
for ($i = 0; $i < 50; $i ++) {
Article::create([
'title' => $faker->sentence,
'body' => $faker->paragraph,
]);
}
}
}
User Seeder
<?php
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
// Let's clear the user table first
User::truncate();
$faker = \Faker\Factory::create();
// Let's make sure everyone has the same password and
// let's hash it before the loop, or else our seeder
// will be too slow.
$password = Hash::make('toptal');
User::create([
'name' => 'Administrator',
'email' => 'admin#test.com',
'password' => $password,
]);
// And now let's generate a few dozen users for our app:
for ($i = 0; $i < 10; $i ++) {
User:;create([
'name' => $faker->name,
'email' => $faker->email,
'password' => $password,
]);
}
}
}
Database Seeder
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$this->call(UsersTableSeeder::class);
$this->call(ArticlesTableSeeder::class);
}
}
First you should import the full class path, i.e.- App\User. Then regenerate the autoload file with- composer dump-autoload
You should either import the models that you've use so you can use just the Model's class name in your code or use the fully qualified name of the Model.
E.g., instead of just User, use App\User.
Use imports if you think you will have many instance where you will use the User class name, to avoid the hassle of typing the fully qualified name.
<?php
...
use App\User;
...
$users = User::all(); // <-- now you can do this.
I followed the same tutorial. Just add a line "use App\Article;" so that your class will find the appropriate class.
Its like including a header file path in c/c++.

Laravel 5.5 Model not found in Seed class

I have been around this problem for so long and cannot solve it... I found several people with (apparently) the same problem as me, but any of the answers helped me.
I have the following "Sector.php" inside "app" folder:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Sector extends Model
{
protected $table = 'sectors';
protected $fillable = ['name'];
protected $guarded = ['id'];
public function services()
{
return $this->belongsToMany('App\Service', 'services_sectors', 'sector_id', 'service_id');
}
public function observations()
{
return $this->belongsToMany('App\Observation', 'observations_sectors', 'sector_id', 'observation_id');
}
}
And the following "DatabaseSeeder.php" inside "database/seeds":
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('sectors')->delete();
Sector::create(['name' => 'Health']);
$this->command->info('Sectors table seeded');
}
}
So, when I access my server I run the command php artisan db:seed but I have the following error:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'Sector' not found
I have been trying ./composer update, ./composer dump-autoload -o, changing Sector to App\Sector in the seeder file but the error just changes to Class 'App\Sector' not found.
If I add use App\Sector; to the top of the Seeder file the error is the same.
It seems I tried all the solutions that are online, so maybe I have some configuration done incorrectly? Any suggestions on this?
Try adding use App\Sector; to your seeding file.
Once you have it working, think about separating your seeding files into their separate classes. It is much easier to maintain that way.
Generate Seeder File
First, in terminal, generate a new seed file:
php artisan make:seeder SectorsTableSeeder
Transfer your seeding code into the run method of this new file.
Call seeder files
Then, modify the DatabaseSeeder.php file to run the SectorsTableSeeder class. For example:
public function run()
{
$this->call(SectorsTableSeeder::class);
}
Update
Sorry, I missed that part.
This is what I would try:
$now = date('Y-m-d H:i:s');
public function run()
{
DB::table('sectors')->delete();
DB::table('sectors')->insert([
'name' => 'Health',
'created_at' => $now,
'updated_at' => $now,
]);
$this->command->info('Sectors table seeded');
}

Laravel DB Seeder won't seed in console

Hi I have the following seeder class I am trying to seed. When I run the php artisan db:seed command the only thing that seeds is my previous seeder class I created a few weeks ago. I have no idea what I am missing. I inserted SoftDeletes and Protected fillables as well.
Here is my seeder class:
public function run()
{
DB::table('leave_type')->insert([
[
'leaveType' => 'Vacation Leave'
],
[
'leaveType' => 'Sick Leave'
],
[
'leaveType' => 'Afternoon Off'
],
[
'leaveType' => 'Special Leave'
],
[
'leaveType' => 'Study Leave'
],
]);
}
My model:
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class LeaveType extends Model
{
protected $fillable = ['leaveType'];
protected $table ="leave_type";
use SoftDeletes;
public $timestamps = true;
}
Converting my comment to answer;
Make sure laravel knows about the new database seeder class you've generated by running this command:
composer dump-auto
Make sure your seeder class is registered in {PROJECT}/database/seeds/DatabaseSeeder.php like this:
$this->call(YourNewSeeder::class);
Then you could refresh the database (rollback all migration, re-run the migration) and run the seeder in one go with this command:
php artisan migrate:refresh --seed
or just run the specific seeder only like this:
php artisan db:seed --class=YourNewSeeder

Categories