From a2be833d4619291a59d6763f0afca7113f3c266c Mon Sep 17 00:00:00 2001 From: Matt Young Date: Wed, 28 Jan 2026 00:37:47 -0600 Subject: [PATCH] Create factories --- app/Models/Client.php | 3 +++ app/Models/Contact.php | 6 ++--- app/Models/Product.php | 3 +++ database/factories/ClientFactory.php | 33 +++++++++++++++++++++++++++ database/factories/ContactFactory.php | 29 +++++++++++++++++++++++ database/factories/ProductFactory.php | 27 ++++++++++++++++++++++ 6 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 database/factories/ClientFactory.php create mode 100644 database/factories/ContactFactory.php create mode 100644 database/factories/ProductFactory.php diff --git a/app/Models/Client.php b/app/Models/Client.php index a33c830..9db993a 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -3,12 +3,15 @@ namespace App\Models; use App\Enums\ClientStatus; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; class Client extends Model { + use HasFactory; + protected $fillable = [ 'name', 'abbreviation', diff --git a/app/Models/Contact.php b/app/Models/Contact.php index 42d1fc6..f463287 100644 --- a/app/Models/Contact.php +++ b/app/Models/Contact.php @@ -3,12 +3,14 @@ namespace App\Models; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; -use Illuminate\Database\Eloquent\Relations\HasMany; class Contact extends Model { + use HasFactory; + public $fillable = ['first_name', 'last_name', 'email', 'phone']; public function clients(): BelongsToMany @@ -20,6 +22,4 @@ class Contact extends Model { return Invoice::whereIn('client_id', $this->clients()->pluck('clients.id')); } - - } diff --git a/app/Models/Product.php b/app/Models/Product.php index 36b37fe..5b4260f 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -3,11 +3,14 @@ namespace App\Models; use App\Casts\MoneyCast; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; class Product extends Model { + use HasFactory; + protected $fillable = [ 'active', 'sku', diff --git a/database/factories/ClientFactory.php b/database/factories/ClientFactory.php new file mode 100644 index 0000000..da78828 --- /dev/null +++ b/database/factories/ClientFactory.php @@ -0,0 +1,33 @@ + $this->faker->name(), + 'abbreviation' => $this->faker->word(), + 'audition_date' => $this->faker->dateTimeBetween('+5 days', '+1 year'), + 'status' => ClientStatus::ACTIVE, + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now(), + ]; + } + + public function withContact(?Contact $contact = null): static + { + return $this->afterCreating(function (Client $client) use ($contact) { + $client->contacts()->attach($contact ?? Contact::factory()->create()); + }); + } +} diff --git a/database/factories/ContactFactory.php b/database/factories/ContactFactory.php new file mode 100644 index 0000000..44a4b31 --- /dev/null +++ b/database/factories/ContactFactory.php @@ -0,0 +1,29 @@ + $this->faker->firstName(), + 'last_name' => $this->faker->lastName(), + 'email' => $this->faker->unique()->safeEmail(), + 'phone' => $this->faker->phoneNumber(), + ]; + } + + public function withClient(?Client $client = null): static + { + return $this->afterCreating(function (Contact $contact) use ($client) { + $contact->clients()->attach($client ?? Client::factory()->create()); + }); + } +} diff --git a/database/factories/ProductFactory.php b/database/factories/ProductFactory.php new file mode 100644 index 0000000..2499bbc --- /dev/null +++ b/database/factories/ProductFactory.php @@ -0,0 +1,27 @@ + true, + 'sku' => $this->faker->unique()->bothify('???-####'), + 'name' => $this->faker->words(3, true), + 'description' => $this->faker->sentence(), + 'price' => $this->faker->numberBetween(1000, 50000), + ]; + } + + public function inactive(): static + { + return $this->state(['active' => false]); + } +} \ No newline at end of file