Laravel 9 - Error Class BooksSeeder does not exist - laravel

When i run php artisan make:seeder --class=BooksSeeder I am getting the following error:
The "--class" option does not exist.
Can anyone help me out?
I'm still new to Laravel
My DatabaseSeeder Class:
<?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()
{
// \App\Models\User::factory(10)->create();
}
}
My BooksSeeder Class:
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\Books;
class BooksSeeder extends Seeder
{
public function run()
{
$faker = \Faker\Factory::create();
for ($i = 0; $i < 50; $i++) {
Books::create([
'name' => $faker->sentence,
'author' => $faker->name,
'publish_date' => $faker->date,
]);
}
}
}

You are using wrong syntax.
To create seeder, use:
php artisan make:seeder BooksSeeder
To run seeder, use:
php artisan db:seed --class=BooksSeeder

Related

laravel-8 user table seeder does not exist

I am trying to make a login from laravel 8 but at the begging I faced an error which I cannot find a solution. The UsersTablesSeeder is created but still the compiler cannot find it
Illuminate\Contracts\Container\BindingResolutionException
Target class [UsersTablesSeeder] does not exist.
at C:\xampp\htdocs\pary\vendor\laravel\framework\src\Illuminate\Container\Container.php:832
828▕
829▕ try {
830▕ $reflector = new ReflectionClass($concrete);
831▕ } catch (ReflectionException $e) {
➜ 832▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
833▕ }
834▕
835▕ // If the type is not instantiable, the developer is attempting to resolve
836▕ // an abstract type such as an Interface or Abstract Class and there is
1 C:\xampp\htdocs\pary\vendor\laravel\framework\src\Illuminate\Container\Container.php:830
ReflectionException::("Class "UsersTablesSeeder" does not exist")
2 C:\xampp\htdocs\pary\vendor\laravel\framework\src\Illuminate\Container\Container.php:830
ReflectionClass::__construct("UsersTablesSeeder")
the following code shows 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()
{
Eloquent::unguard();
$this->call(UsersTablesSeeder::class);
}
}
this is my user table
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\User;
class UsersTablesSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
User::create([
'name' => 'John Smith',
'email' => 'john_smith#gmail.com',
'password' => Hash::make('password'),
'remember_token' => str_random(10),
]);
}
}
I am following this link
Add namespace Database\Seeders; to your class. As said in laravel 8
Seeders and factories are now namespaced. To accommodate for these
changes, add the Database\Seeders namespace to your seeder classes. In
addition, the previous database/seeds directory should be renamed to
database/seeders:

Getting class does not exist error when running database seeder

I am creating a seeder in laravel 6.1 but I keep getting this error
Illuminate\Contracts\Container\BindingResolutionException : Target class [AdminsTableSeeder] does not exist.
I tried running composer dump-autoload and composer dumpautoload, it doesn't work for me.
here is my AdminsTableSeeder.php
use App\Models\Admin;
use Faker\Factory as Faker;
use Illuminate\Database\Seeder;
class AdminsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$faker = Faker::create();
Admin::create([
'name' => $faker->name,
'email' => 'admin#admin.com',
'password' => bcrypt('password'),
]);
}
}
and here is my DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
$this->call(AdminsTableSeeder::class);
}
}
Make sure your AdminsTableSeeder.php file is in the same directory where you have your DatabaseSeeder.php file.
Run
composer dump-autoload
then try
php artisan db:seed
Optional.
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run(){
$this->call('AdminsTableSeeder');
}
}
try with $this->call('AdminsTableSeeder'); like this.
In your case, move all seeder files from previous database/seeds directory to database/seeders folder & then run composer dump-autoload.
Remember, from laravel 8 seeders and factories are namespaced
To accommodate for these changes,
[1] - Add Database\Seeders namespace to your seeder classes.
namespace Database\Seeders;
[2] - Move all seeder files to database/seeders folder.
[3] - If you import any seeders classes in DatabaseSeeder file then remove all of them. (simply remove all lines that started with use Database\Seeders\... from DatabaseSeeder.php)
[4] - Finally run dump-autoload.
composer dump-autoload
You can now try a fresh migration with seed,
php artisan migrate:fresh --seed
For my case(I use Laravel 8), I solved my problem by modifying the RouteServiceProvider.php file in App/Providers/ path. I uncommented code on line 29.
protected $namespace = 'App\\Http\\Controllers';
It worked for me.
run
composer dump-autoload
then try
php artisan db:seed
For Laravel 8:
I have the same issue and I found a solution in Laravel doc and it's worked for me.
https://laravel.com/docs/8.x/upgrade#seeder-factory-namespaces
Update Composer:
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
}
Run:
composer dumpautoload
php artisan db:seed --force
Concerning my case, I used the latest Laravel 8 which is the latest version, I solved my problem by changing the RouteServiceProvider.php file in App/Providers/ path by uncommenting the code on line 29.
protected $namespace = 'App\Http\Controllers';
For Laravel ^7.0
If your using Laravel Eloquent
Example:
<?php
use App\Models\User;
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
public function run()
{
$users = [
[
'id' => 1,
'name' => 'Admin',
'email' => 'admin#admin.com',
'password' => bcrypt('password'),
'remember_token' => null,
],
];
User::insert($users);
}
}
If your using Laravel Query Builder
Example:
<?php
//Do not use -> namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class UsersTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->insert([
'name' => 'Admin',
'email' => 'admin#admin.com',
'password' => bcrypt('password'),
'remember_token' => null,
]);
}
}
In your DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call([
UsersTableSeeder::class,
]);
}
}
Seems like the controller name is case-sensitive in Laravel 8. So my suggestion is to double-check the controller name.
For instance:
in web.php avoid calling
UserAPIController
as
UserApiController
(API as api)
It may fix this error.
In your DatabaseSeeder.php, you can add the nameSpace for AdminsTableSeeder like -
use App\Models\Admin\AdminsTableSeeder;
Closing the current running serve before doing db:seeds

how to fix fullCalendar

Non-static method MaddHatter\LaravelFullcalendar\Calendar::addEvents() should not be called statically
I tried to use
* composer dump-autoload
* php artisan cache:clear
* php artisan config:clear
did not help
EventsController.php
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Event;
use MaddHatter\LaravelFullcalendar\Calendar;
class EventsController extends Controller
{
public function index(){
$events = Event::get();
$event_list = [];
foreach ($events as $key => $event) {
$event_list[] = Calendar::event(
$event->event_name,
true,
new \DateTime($event->start_date),
new \DateTime($event->end_date.' +1 day')
);
}
$calendar_details = Calendar::addEvents($event_list);
return view('events', compact('calendar_details') );
}
Calendar.php
public function addEvents($events, array $customAttributes = [])
{
foreach ($events as $event) {
$this->eventCollection->push($event, $customAttributes);
}
return $this;
}
Could someone help me?
You need to make an instance of your calendar first
change:
$calendar_details = Calendar::addEvents($event_list);
return view('events', compact('calendar_details') );
to:
$calendar_details = new Calendar($your_construction_parameters);
$calendar_details->addEvents($event_list);
return view('events', compact('calendar_details'));

laravel 5.4: seeding for inheritance model return error

I have created base model and extend all my model from base model in laravel 5.4. When i do db:seed i got error
Trying to get property of non-object
. Anyone know why it happens? it is db:seed did not support model inheritance.
Base Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Auth;
class BaseModel extends Model
{
public static function boot()
{
parent::boot();
static::creating(function($model)
{
$model->created_by = Auth::user()->id;
$model->updated_by = Auth::user()->id;
});
static::updating(function($model)
{
$model->updated_by = Auth::user()->id;
});
static::deleting(function($model)
{
$model->deleted_by = Auth::user()->id;
$model->save();
});
}
}
Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\SoftDeletes;
class Bank extends BaseModel
{
use SoftDeletes;
public static function boot()
{
parent::boot();
}
}
Seeder:
<?php
use Illuminate\Database\Seeder;
use App\Bank as Bank;
class BanksTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Bank::create( [
'name' => 'xxxxxxxx' ,
] );
}
}
Probably it has to do with Auth::user()->id. db:seed is executed in terminal and has no authenticated user, therefore Auth::user() will return NULL. Do a check before setting created_by and updated_by.
static::creating(function($model)
{
if (Auth::user())
{
$model->created_by = Auth::user()->id;
$model->updated_by = Auth::user()->id;
}
});
Hope this helps :)

Laravel Seeder for Different Environments

I created two folders in my seeder folder:
/seeds
/local
/production
DatabaseSeeder.php
Then, defined the following inside DatabaseSeeder.php
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Eloquent::unguard();
// Load production seeder
if (App::Environment() === 'production')
{
$this->call('production/UsersTableSeeder');
}
// Load local seeder
if (App::Environment() === 'local')
{
$this->call('local/UsersTableSeeder');
}
}
}
Now I know I can't do call('local/UsersTablderSeeder'), and that is my question. How can I call() the seeder files from their respective folders?
Edit
To be clear, when I run the code as it is shown above, I get the following error
[ReflectionException]
Class local/UsersTableSeeder does not exist
I just tried this quickly and got it working, so I'll show you how I set it up and hopefully that helps.
app/database/seeds/local/UsersTableSeeder.php
<?php namespace Seeds\Local;
use Illuminate\Database\Seeder as Seeder;
Class UsersTableSeeder extends Seeder {
public function run () {
dd('local');
}
}
app/database/seeds/production/UsersTableSeeder.php
<?php namespace Seeds\Production;
use Illuminate\Database\Seeder as Seeder;
Class UsersTableSeeder extends Seeder {
public function run () {
dd('production');
}
}
app/database/seeds/DatabaseSeeder.php
<?php
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* #return void
*/
public function run(){
Eloquent::unguard();
// Load production seeder
if (App::Environment() === 'production')
{
$this->call('Seeds\Production\UsersTableSeeder');
}
// Load local seeder
if (App::Environment() === 'local')
{
$this->call('Seeds\Local\UsersTableSeeder');
}
}
}
And don't forget to run composer dump-autoload.
Hope that helps.
Laravel 5.7 or higher
if ( App::environment('local') ) {
$this->call(Seeder::class);
}
if ( App::environment('production') ) {
$this->call(Seeder::class);
}
if ( App::environment('testing') ) {
$this->call(Seeder::class);
}
The problem is '/'. You should use '\' instead.

Resources