Created
November 27, 2021 00:04
-
-
Save NandoKstroNet/01d8bfba3e834171d1e36478064904c9 to your computer and use it in GitHub Desktop.
Factories e Seeder para criação de alguns dados fakes para teste do projeto Meus Gastos criado no curso Livewire na Prática. Em http://codeexperts.com.br/curso/livewire-na-pratica
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Database\Seeders; | |
use Illuminate\Database\Seeder; | |
class DatabaseSeeder extends Seeder | |
{ | |
/** | |
* Seed the application's database. | |
* | |
* @return void | |
*/ | |
public function run() | |
{ | |
$plans = \App\Models\Plan::factory(10)->create(); | |
$plans->each(function($plan) { | |
\App\Models\User::factory(10) | |
->has( | |
\App\Models\UserPlan::factory() | |
->state(function($attributes) use ($plan){ | |
return ['plan_id' => $plan->id]; | |
}), 'plan') | |
->hasExpenses(10) | |
->create(); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Database\Factories; | |
use App\Models\Expense; | |
use Illuminate\Database\Eloquent\Factories\Factory; | |
class ExpenseFactory extends Factory | |
{ | |
/** | |
* The name of the factory's corresponding model. | |
* | |
* @var string | |
*/ | |
protected $model = Expense::class; | |
/** | |
* Define the model's default state. | |
* | |
* @return array | |
*/ | |
public function definition() | |
{ | |
return [ | |
'description' => $this->faker->sentence(), | |
'amount' => $this->faker->randomFloat(2, 1, 1000), | |
'expense_date' => $this->faker->dateTimeBetween('-30 days')->format('d/m/Y H:i:s'), | |
'type' => $this->faker->numberBetween(1, 2), | |
]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Database\Factories; | |
use App\Models\Plan; | |
use Illuminate\Database\Eloquent\Factories\Factory; | |
class PlanFactory extends Factory | |
{ | |
/** | |
* The name of the factory's corresponding model. | |
* | |
* @var string | |
*/ | |
protected $model = Plan::class; | |
/** | |
* Define the model's default state. | |
* | |
* @return array | |
*/ | |
public function definition() | |
{ | |
return [ | |
'name' => $this->faker->word, | |
'description' => $this->faker->sentence, | |
'slug' => $this->faker->slug, | |
'reference' => 'xpto-' . rand(1, 10000), | |
'price' => ($this->faker->randomFloat(2, 1, 200)) * 100 | |
]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Database\Factories; | |
use App\Models\UserPlan; | |
use Illuminate\Database\Eloquent\Factories\Factory; | |
class UserPlanFactory extends Factory | |
{ | |
/** | |
* The name of the factory's corresponding model. | |
* | |
* @var string | |
*/ | |
protected $model = UserPlan::class; | |
/** | |
* Define the model's default state. | |
* | |
* @return array | |
*/ | |
public function definition() | |
{ | |
return [ | |
'reference_transaction' => $this->faker->uuid | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment