From a6c978981d4957649ca46373c713fa9ffbe8b434 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Mon, 29 Mar 2021 05:15:33 -0700 Subject: [PATCH] Add Goal factory --- app/Http/Controllers/GoalController.php | 2 +- app/Models/Goal.php | 4 +++ database/factories/GoalFactory.php | 38 +++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 database/factories/GoalFactory.php diff --git a/app/Http/Controllers/GoalController.php b/app/Http/Controllers/GoalController.php index ad0b02e..fd6b0ad 100644 --- a/app/Http/Controllers/GoalController.php +++ b/app/Http/Controllers/GoalController.php @@ -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'], ]); diff --git a/app/Models/Goal.php b/app/Models/Goal.php index cdbacb6..2bd35a2 100644 --- a/app/Models/Goal.php +++ b/app/Models/Goal.php @@ -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. */ diff --git a/database/factories/GoalFactory.php b/database/factories/GoalFactory.php new file mode 100644 index 0000000..5875b25 --- /dev/null +++ b/database/factories/GoalFactory.php @@ -0,0 +1,38 @@ +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, + ]; + } +}