From 74fe2be70e4f30907994f3cfdd739ae9d4f0789d Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sat, 13 Feb 2021 05:37:09 -0800 Subject: [PATCH] Add "Goals" model --- app/Models/Goal.php | 63 +++++++++++++++++++ app/Models/JournalEntry.php | 2 +- app/Models/User.php | 11 +++- .../2021_02_13_052427_create_goals_table.php | 38 +++++++++++ 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 app/Models/Goal.php create mode 100644 database/migrations/2021_02_13_052427_create_goals_table.php diff --git a/app/Models/Goal.php b/app/Models/Goal.php new file mode 100644 index 0000000..750d914 --- /dev/null +++ b/app/Models/Goal.php @@ -0,0 +1,63 @@ + 'datetime:Y-m-d', + 'to' => 'datetime:Y-m-d', + 'goal' => 'float', + ]; + + /** + * Get the User this goal belongs to. + */ + public function user(): BelongsTo { + return $this->belongsTo(User::class); + } +} diff --git a/app/Models/JournalEntry.php b/app/Models/JournalEntry.php index ecdbe34..ad678e7 100644 --- a/app/Models/JournalEntry.php +++ b/app/Models/JournalEntry.php @@ -69,7 +69,7 @@ final class JournalEntry extends Model ]; /** - * The attributes that should be cast. + * @inheritdoc */ protected $casts = [ 'calories' => 'float', diff --git a/app/Models/User.php b/app/Models/User.php index 31736f9..667603a 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -2,8 +2,8 @@ namespace App\Models; -use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -32,6 +32,8 @@ use Illuminate\Notifications\Notifiable; * @method static \Illuminate\Database\Eloquent\Builder|User whereRememberToken($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereUpdatedAt($value) * @mixin \Eloquent + * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Goal[] $goals + * @property-read int|null $goals_count */ final class User extends Authenticatable { @@ -60,4 +62,11 @@ final class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + /** + * Get the User's goals. + */ + public function goals(): HasMany { + return $this->hasMany(Goal::class); + } } diff --git a/database/migrations/2021_02_13_052427_create_goals_table.php b/database/migrations/2021_02_13_052427_create_goals_table.php new file mode 100644 index 0000000..e3bd190 --- /dev/null +++ b/database/migrations/2021_02_13_052427_create_goals_table.php @@ -0,0 +1,38 @@ +id(); + $table->foreignIdFor(User::class)->constrained()->cascadeOnUpdate()->cascadeOnDelete(); + $table->date('from')->nullable(); + $table->date('to')->nullable(); + $table->string('attribute'); + $table->unsignedFloat('goal'); + $table->timestamps(); + $table->index('user_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('goals'); + } +}