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
Related
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);
}
}
I'm working with seeder to fill my users table, so I created a new seeder called UserSeeder and then I added these codes to it:
public function run()
{
foreach(range(1,10) as $item)
{
DB::table('users')->insert([
'name' => "name $item",
'email' => "email $item",
'email_verified_at' => now(),
'password' => "password $item"
]);
}
}
After that I tried php artisan db:seed --class=UserSeeder but it shows me:
Error
Class 'Database\Seeders\DB' not found
which is related to this line:
DB::table('users')->insert([
So why it is not found there, what should I do now?
That's because Laravel will look for DB class in the current namespace which is Database\Seeders.
Since Laravel has facades defined in config/app.php which allows you to use those classes without full class name.
'DB' => Illuminate\Support\Facades\DB::class,
You can either declare DB class after the namespace declaration with
use DB;
or just use it with backslash.
\DB::table('users')->insert([
In the UserSeeder Class add:
use Illuminate\Support\Facades\DB;
I have fixed same error in Laravel 9 by importing
use Illuminate\Database\Seeder;
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 :)
I have a class User that extends
<?php
namespace App;
class User extends \Cartalyst\Sentinel\Users\EloquentUser
{
public function chalets(){
return $this->hasMany('App\Chalet');
}
}
and i have Chalet Class
class Chalet extends Model
{
protected $fillable = [
'name', 'description',
];
public function user(){
return $this->belongsTo('App\User');
}
}
And i have method to add chalet by user :
public function postCreateChalet(Request $request){
$chalet = new Chalet([
'name' => $request->input('name'),
'description' => $request->input('description')
]);
Sentinel::getUserRepository()->setModel('App\User');
$user = Sentinel::getUser();
$user->chalets()->save($chalet);
return ('chalet has created');
}
and its give me an error :
BadMethodCallException
Call to undefined method Cartalyst\Sentinel\Users\EloquentUser::chalets()
Is it a right way to extend User class ?
I have searched for ways to extend the User class. I found this question:Model Inheritance in Laravel didn't help me though.
I'm using Laravel 5.7
The exception you're getting indicates Sentinel is still referring to the default stock Sentinel's EloquentUser model. Make sure you point to your extended user model with the published Sentinel configurations.
Run the below command
php artisan vendor:publish --provider="Cartalyst\Sentinel\Laravel\SentinelServiceProvider"
Open up the published config file at 'config\cartalyst.sentinel.php'
Modify it from the below content:
'users' => [
'model' => 'Cartalyst\Sentinel\Users\EloquentUser',
],
to:
'users' => [
'model' => 'App\User',
],
For more information, refer to https://github.com/cartalyst/sentinel/wiki/Extending-Sentinel
You won't need the following line after you configured it via config:
Sentinel::getUserRepository()->setModel('App\User');
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');
}