Add Goal factory

This commit is contained in:
Christopher C. Wells 2021-03-29 05:15:33 -07:00 committed by Christopher Charbonneau Wells
parent 8fe730bb14
commit a6c978981d
3 changed files with 43 additions and 1 deletions

View File

@ -75,7 +75,7 @@ class GoalController extends Controller
$attributes = $request->validate([
'from' => ['nullable', 'date'],
'to' => ['nullable', 'date'],
'frequency' => ['nullable', 'string'],
'frequency' => ['required', 'string'],
'name' => ['required', 'string'],
'goal' => ['required', 'numeric'],
]);

View File

@ -3,6 +3,7 @@
namespace App\Models;
use App\Support\Nutrients;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@ -33,9 +34,12 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereUserId($value)
* @mixin \Eloquent
* @method static \Database\Factories\GoalFactory factory(...$parameters)
*/
final class Goal extends Model
{
use HasFactory;
/**
* Supported options for thr frequency attribute.
*/

View File

@ -0,0 +1,38 @@
<?php
namespace Database\Factories;
use App\Models\Goal;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class GoalFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Goal::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
/** @var \App\Models\User $user */
$user = User::factory()->create();
$from = $this->faker->dateTimeThisMonth;
$to = $this->faker->dateTimeBetween($from, '+1 year');
return [
'from' => $this->faker->randomElement([$from, null]),
'to' => $this->faker->randomElement([$to, null]),
'frequency' => $this->faker->randomElement(Goal::$frequencyOptions)['value'],
'name' => $this->faker->randomElement(Goal::getNameOptions())['value'],
'goal' => $this->faker->numberBetween(0, 2000),
'user' => $user,
];
}
}