Category : factory

I Have a question. I’m working with a Laravel project that only has models, the corresponding migrations are in other "micro projects". I’m developing TDD, and in many cases I need to use factories to test some functionalities…but, when I run the test, messages like this, appears: General error: 1 no such table: users Can ..

Read more

I want take a random id already existing from my garage table, so i have a problem. <?php namespace DatabaseFactories; use AppModelsCar; use AppModelsGarage; use IlluminateDatabaseEloquentFactoriesFactory; class CarFactory extends Factory { /** * The name of the factory’s corresponding model. * * @var string */ protected $model = Car::class; /** * Define the model’s default ..

Read more

I am trying to retrieve a date from an object (it returns a string), change it to a DateTime, then add a month and turn it back into a string all inside an observer. Below is the Observer code: $date = $loans->startDate; $start = date_create($date); $startdate = $start->modify(‘+1 months’); $stringstart = $start->format(‘Y-m-d H:i:s’); LoanPeriods::factory([ ‘loans_id’ ..

Read more

I have errors executing the command:: >php artisan migrate:refresh –seed The error message is: BadMethodCallException Call to undefined method AppModelsDiary::diarySubjob() at C:MAMPhtdocsdominiosvendorlaravelframeworksrcIlluminateSupportTraitsForwardsCalls.php:50 46▕ * @throws BadMethodCallException 47▕ */ 48▕ protected static function throwBadMethodCallException($method) 49▕ { ➜ 50▕ throw new BadMethodCallException(sprintf( 51▕ ‘Call to undefined method %s::%s()’, static::class, $method 52▕ )); 53▕ } 54▕ } • ..

Read more

Here my UserFactory : <?php namespace DatabaseFactories; use AppUser; use FakerGenerator as Faker; use FakerFactory as FakerFactory; use IlluminateDatabaseEloquentFactoriesFactory; use IlluminateSupportStr; $factory->define(User::class, function (Faker $faker) { static $password; return [ ‘firstname’ => $this->faker->firstName(), ‘name’ => $this->faker->lastName, ‘nickname’ => $this->faker->userName, ’email’ => $this->faker->unique()->safeEmail, ‘password’ => $password ?: $password = bcrypt(‘secret’), ‘remember_token’ => Str::random(10), ]; }); Here ..

Read more

I would like to know how can I seed the data, when I use a booted function in model. Currently I have this in the model : class Event extends Model { protected $fillable = [‘title’, …]; public static function booted() { static::creating(function ($event) { $event->user_id = auth()->id(); }); } … } In the factory, ..

Read more

I have a factory for generating the event public function definition() { return [ ‘event_type’ => $this->faker->randomElement([‘class’,’event’]), ‘event_name’ => $this->faker->sentence(1,2), ‘date’ => $this->faker->dateTimeBetween($startDate = ‘+1 days’, $endDate = ‘+5 days’), ‘start_time’ => $this->faker->dateTimeBetween(‘+0 hours’, ‘+2 hours’), ‘end_time’ => $this->faker->dateTimeBetween(‘+3 hours’, ‘+10 hours’), ]; } Here I need start_time and date to have the same value ..

Read more

I have here 2 columns I only want to add dummy data, and not include other columns on my model factory file. Will that possible? I’ve used faker to add dummy data, and Picqr php barcode generator. Below is my factory file: <?php namespace DatabaseFactories; use AppModelsProduct; use IlluminateDatabaseEloquentFactoriesFactory; use Picqer; class ProductFactory extends Factory ..

Read more

I have a many to many relationship between two models: User namespace AppModels; use IlluminateDatabaseEloquentFactoriesHasFactory; use IlluminateDatabaseEloquentModel; use IlluminateFoundationAuthUser as Authenticatable; use IlluminateNotificationsNotifiable; use IlluminateNotificationsNotification; use IlluminateSupportFacadesHash; use LaravelPassportHasApiTokens; class User extends Model { use HasApiTokens, Notifiable,hasFactory; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ ’email’, ..

Read more

I’m using ADR architecture with Laravel in my new project. I’ve created ADR for the User which contains the Actions, Domain, Responder folder. I’ve defined User Model In CoreUserDomainEntities which has code like below: class User extends Authenticatable implements JWTSubject { use HasFactory, Notifiable; /** * The attributes that are mass assignable. * * @var ..

Read more

we are currently working on a laravel 8 application. We are trying to create factories to create some dummy data for manual / developer based application testing. The current code of my main Database-Seeder is below: class DatabaseSeeder extends Seeder { public function run() { $this->call([ UserTableSeeder::class, ]); AppModelsUser::factory(10)->create(); AppModelsActivity::factory(5)->create(); /* 1. try $tenFact = ..

Read more

I have two factories one for categories and another for products. When I run the factory I want to create x number of products for each category generated. how would I write the code to product this? the definition for the categories is written as this: return [ ‘name’ => $this->faker->word, ‘slug’ => Str::slug($this->faker->unique()->word, ‘-‘), ..

Read more

I’m Inserting some dummy data using the factory model class using this command on tinker: When i run composer dump-autoload, php artisan tinker, Page::factory(10)->create() Then this error shows PHP Error: Class ‘Page’ not found in /var/www/html/laravel/laravel8-blogeval()’d code on line 1 enter image description here My model file location appModelsPage.php <?php namespace AppModels; use IlluminateDatabaseEloquentFactoriesHasFactory; use ..

Read more