recently i start to practice laravel/lumen. Everything was fine but now i am facing a problem when i am going to try the command: php artisan db:seed It is showing me a error that: Class 'Database\Seeders\lumenPractice' not found
i also tried: php artisan migrate:fresh --seed it is also not working and showing me the same error. i am using php version 7.4.11enter code here
my LumenPractice.php code are given below:
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
class LumenPractice extends Model
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name',
'gender',
'country',
];
}
My DatabaseSeeder.php code are given below:
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
//factory(lumenPractice::class(), 50)->create();
lumenPractice::factory(count(30))->create();
// $this->call('UsersTableSeeder');
}
}
My UserFactory.php code are given below:
<?php
namespace Database\Factories;
use App\Models\LumenPractice;
use Illuminate\Database\Eloquent\Factories\Factory;
class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* #var string
*/
protected $model = LumenPractice::class;
/**
* Define the model's default state.
*
* #return array
*/
public function definition()
{
return [
'gender' =>$gender = $this->faker->randomElement(['male''female'])
'name' => $this->faker->name($gender),
'country' => $this->faker->country,
];
}
}
Have you tried capitalizing the "l" in lumenPractice::factory(count(30))->create();? I believe you meant to write LumenPractice::factory(count(30))->create(); . Everything else seems fine.
Related
I'm trying to seed two tables with the following code:
<?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Foundation\Auth\User;
use App\Models\Role;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
User::factory(10)->create();
$roles = [
'dps',
'tank',
'healer'
];
foreach ($roles as $role) {
Role::create([
'name' => $role,
]);
}
foreach(User::all() as $user) {
foreach (Role::all() as $role) {
$user->$roles()->attach($role->id);
}
}
}
}
using php artisan db:seed, but its returning Call to undefined method Illuminate\Foundation\Auth\User::factory().
Going through User model, I have the following:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
/**
* The attributes that are mass assignable.
*
* #var string[]
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* #var array
*/
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];
/**
* The attributes that should be cast.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* The accessors to append to the model's array form.
*
* #var array
*/
protected $appends = [
'profile_photo_url',
];
public function monstros() {
return $this->hasMany('App\Models\Monstro');
}
public function arsenais() {
return $this->hasMany('App\Models\Arsenal');
}
public function itens() {
return $this->hasMany('App\Models\Item');
}
public function roles() {
return $this->belongsToMany(Role::class);
}
}
Where we can clearly see it's calling for the Factory method with use HasFactory
What am I missing here?
Also tried to restart artisan, update the framework, dump-auto load, none of those helped.
You are calling the wrong class instead of calling the App\Models\User you are calling the Illuminate\Foundation\Auth\User. Which has the same name. You can take a look at the namespaces above.
I'm creating factories in my Laravel 8 project, I've used them before so am quite familiar with their set up.
In my project, I'm having trouble getting Laravel to pick up my factories and cannot figure out why, the error I'm getting is that my factory class can't be found, I've tried composer dump-autoload and also have tried various cache clearing commands with no result.
What am I missing?
My database/factories/BrandFactory is:
<?php
namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Carbon\Carbon;
class BrandFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* #var string
*/
protected $model = Brand::class;
/**
* Define the model's default state.
*
* #return array
*/
public function definition()
{
$todo = $this->faker->unique()->company();
$slug = Str::slug($brand);
return [
'user_id' => User::all()->random()->id,
'brand' => $brand,
'slug' => $slug,
'url' => $this->faker->domain(),
'created_at' => Carbon::now()->subDays(rand(1, 14))
];
}
}
I have HasFactory on my model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Brand extends Model
{
use HasFactory, SoftDeletes;
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'brands';
/**
* The attributes that are mass assignable.
*
* #var string[]
*/
protected $fillable = [
'brand',
'url'
];
/**
* The relationships that should always be loaded.
*
* #var array
*/
protected $with = [
'form'
];
/**
* Get the form associated with the user.
*/
public function form()
{
return $this->hasOne(Form::class);
}
/**
* Get the brand that owns the comment.
*/
public function brand()
{
return $this->belongsTo(User::class);
}
}
Which is called from my seeder:
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\Brand;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
Brand::factory(3)->create();
}
}
Also, since this is a Laravel 8 project, autoloader is configured correctly to:
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
}
Your code look fine, anyway, you can tell your model to find it's own factory using newFactory method:
In your model:
protected static function newFactory()
{
return Database\Factories\BrandFactory::new();
}
I get an error when create a table seeder using model factory in laravel 8 but I don't know where I'm going wrong here.
This is an error:
Undefined constant "App\Models\boolean"
at C:\xampp\htdocs\mason\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php:628
Here is my code:
Category.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Astrotomic\Translatable\Translatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Category extends Model
{
use HasFactory, Translatable;
protected $with = ['translations'];
protected $translatedAttributes = ['name'];
protected $hidden = ['translattions'];
protected $casts = ['is_active' => boolean];
protected $fillable = ['parent_id', 'slug', 'is_active'];
}
CategoryFactory.php:
<?php
namespace Database\Factories;
use App\Models\Category;
use Illuminate\Database\Eloquent\Factories\Factory;
class CategoryFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* #var string
*/
protected $model = Category::class;
/**
* Define the model's default state.
*
* #return array
*/
public function definition(): array
{
return array(
'name' => $this->faker->word(),
'slug' => $this->faker->slug(),
'is_active' => $this->faker->boolean(),
);
}
}
CategoryTableSeeder.php
<?php
namespace Database\Seeders;
use App\Models\Category;
use Illuminate\Database\Seeder;
class CategoryTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Category::factory(10)->create();
}
}
At Eloquent: Mutators & Casting we can read:
The $casts property should be an array where the key is the name of the attribute being cast and the value is the type you wish to cast the column to.
It doesn't mention having constants defined, though you can figure out given that one of the possible values listed is decimal:<digits>, which isn't a valid constant name.
Also, the example shown is:
protected $casts = [
'is_admin' => 'boolean',
];
I get the title error when I run command:
php artisan db:seed
My screenshot:
I have no idea where this problem comes from. I was searching for code examples and solution but I haven't found anything :(
ArticlesTableSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
// use Laracasts\TestDummy\Factory as TestDummy;
class ArticlesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
factory(App\Models\Article::class, 30)->create();
}
}
ArticleFactory.php
<?php
namespace Database\Factories;
use App\Models\Model;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class ModelFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* #var string
*/
protected $model = App\Models\Article::class;
/**
* Define the model's default state.
*
* #return array
*/
public function definition()
{
return [
'title' => $faker->text(50),
'body' => $faker->text(200)
];
}
}
DatabaseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
$this->call(ArticlesTableSeeder::class);
}
}
Thank you in advance for your help!
In laravel 8 the default route namespace was removed.
Try to change:
ArticlesTableSeeder.php:
factory(App\Models\Article::class, 30)->create();
to:
\App\Models\Article::factory()->count(30)->create();
ArticleFactory.php:
protected $model = App\Models\Article::class;
to:
protected $model = \App\Models\Article::class;
and you will probably have to change:
'title' => $faker->text(50),
'body' => $faker->text(200)
to:
'title' => $this->faker->text(50),
'body' => $this->faker->text(200)
All suggestions that were mentioned here are correct.
Sometimes running composer require laravel/legacy-factories might fix your problem if you're using Laravel 8.
Also, in case you get an error that says Class 'Database\Factories\ArticleFactory' not found
then make sure you have class ArticleFactory extends Factory and not ModalFactory.
And make sure you're using HasFactory in the Article Model like here.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use HasFactory;
}
For more info: Laravel 8 Modal Factories
Try to change
factory(App\Models\Article::class, 30)->create();
to
App\Models\Article::factory()->count(30)->create();
ArticlesTableSeeder.php
<?php
namespace Database\Seeders;
use App\Models\Article;
use Illuminate\Database\Seeder;
class ArticlesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Article::factory()->times(30)->create();
}
}
ArticleFactory.php
<?php
namespace Database\Factories;
use App\Models\Article;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class ArticleFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* #var string
*/
protected $model = Article::class;
/**
* Define the model's default state.
*
* #return array
*/
public function definition()
{
return [
'title' => $this->faker->text(50),
'body' => $this->faker->text(200)
];
}
}
If you are using version 8 of laravel, look at the upgrade guide.
"Laravel's model factories feature has been totally rewritten to support classes and is not compatible with Laravel 7.x style factories. However, to ease the upgrade process, a new laravel/legacy-factories package has been created to continue using your existing factories with Laravel 8.x. You may install this package via Composer:
composer require laravel/legacy-factories"
https://laravel.com/docs/8.x/upgrade#seeder-factory-namespaces
change
factory(App\Models\Article::class, 30)->create();
to
\App\Models\Article::factory(30)->create();
I'm facing the same issue when I realized my model was not using the HasFactory trait.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory;
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'categories';
}
So make sure this trait is being used by your model.
If you are using version 8 of laravel you can directory use:
use App\Models\Article;
Article::factory()->count(30)->create();
composer require laravel/legacy-factories
php artisan db:seed
I appreciate this question does appear on here many times, however after looking through the answers and attempting to resolve the issue, it unfortunately still persists.
I am using the Caffeinated Modules for Laravel package with Laravel 5.6. I have created a User module which contains the following.
UserTableSeeder App/Modules/User/Database/Seeds/UserTableSeeder.php
<?php
namespace App\Modules\User\Database\Seeds;
use Illuminate\Database\Seeder;
use App\Modules\User\Models\User;
class UserTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
factory(User::class, 3)->create();
}
}
UserFactory App/Modules/User/Database/Factories/UserFactory.php
<?php
use Faker\Generator as Faker;
use App\Modules\User\Models\User;
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
$factory->define(User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'remember_token' => str_random(10),
];
});
UserDatabaseSeeder App/Modules/User/Database/Seeds/UserDatabaseSeeder.php
<?php
namespace App\Modules\User\Database\Seeds;
use Illuminate\Database\Seeder;
class UserDatabaseSeeder extends Seeder
{
public function run()
{
$this->call(UserTableSeeder::class);
}
}
User App/Modules/User/Models/User.php
<?php
namespace App\Modules\User\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
When I run
php artisan module:seed
the UserDatabaseSeeder calls the UserTableSeeder but produces the following error message:
Seeding: App\Modules\User\Database\Seeds\UserTableSeeder
InvalidArgumentException : Unable to locate factory with name
[default] [App\Modules\User\Models\User].
Any help is much appreciated.
Basically Caffinated Modules doesn't support loading factories from your module out of the box.
I found this issue that includes a fix: https://github.com/caffeinated/modules/issues/337
Add this to your ModuleServiceProvider:
/**
* Register the module services.
*
* #return void
*/
public function register()
{
$this->app->register(RouteServiceProvider::class);
$this->mergeConfigFrom(
__DIR__.'/../config.php',
'user'
);
$this->registerEloquentFactoriesFrom(__DIR__.'/../Database/Factories');
}
/**
* Register factories.
*
* #param string $path
* #return void
*/
protected function registerEloquentFactoriesFrom($path)
{
$this->app->make(Factory::class)->load($path);
}