Create factories
This commit is contained in:
parent
1f383e69a9
commit
a2be833d46
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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'));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Enums\ClientStatus;
|
||||
use App\Models\Client;
|
||||
use App\Models\Contact;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class ClientFactory extends Factory
|
||||
{
|
||||
protected $model = Client::class;
|
||||
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'name' => $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());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Client;
|
||||
use App\Models\Contact;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
class ContactFactory extends Factory
|
||||
{
|
||||
protected $model = Contact::class;
|
||||
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'first_name' => $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());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Product;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
class ProductFactory extends Factory
|
||||
{
|
||||
protected $model = Product::class;
|
||||
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'active' => 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]);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue