Add "Goals" model

This commit is contained in:
Christopher C. Wells 2021-02-13 05:37:09 -08:00
parent 33a8591c72
commit 74fe2be70e
4 changed files with 112 additions and 2 deletions

63
app/Models/Goal.php Normal file
View File

@ -0,0 +1,63 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* App\Models\Goal
*
* @property int $id
* @property int $user_id
* @property \datetime|null $from
* @property \datetime|null $to
* @property string $attribute
* @property float $goal
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \App\Models\User $user
* @method static \Illuminate\Database\Eloquent\Builder|Goal newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Goal newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Goal query()
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereAttribute($value)
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereFrom($value)
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereGoal($value)
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereTo($value)
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereUserId($value)
* @mixin \Eloquent
*/
class Goal extends Model
{
use HasFactory;
/**
* @inheritdoc
*/
protected $fillable = [
'from',
'to',
'attribute',
'goal',
];
/**
* @inheritdoc
*/
protected $casts = [
'from' => '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);
}
}

View File

@ -69,7 +69,7 @@ final class JournalEntry extends Model
];
/**
* The attributes that should be cast.
* @inheritdoc
*/
protected $casts = [
'calories' => 'float',

View File

@ -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);
}
}

View File

@ -0,0 +1,38 @@
<?php
use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateGoalsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('goals', function (Blueprint $table) {
$table->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');
}
}