a2172f0851
Hopefully this will result in slightly better code.
101 lines
1.9 KiB
PHP
101 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
/**
|
|
* @property Account $account
|
|
* @property Contact $contact
|
|
* @method static Builder completed()
|
|
* @method static Builder inProgress()
|
|
*/
|
|
class Task extends Model
|
|
{
|
|
/**
|
|
* The attributes that aren't mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $guarded = ['id'];
|
|
|
|
/**
|
|
* The attributes that should be mutated to dates.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $dates = ['completed_at'];
|
|
|
|
/**
|
|
* Get the account record associated with the task.
|
|
*
|
|
* @return BelongsTo
|
|
*/
|
|
public function account()
|
|
{
|
|
return $this->belongsTo(Account::class);
|
|
}
|
|
|
|
/**
|
|
* Get the contact record associated with the task.
|
|
*
|
|
* @return BelongsTo
|
|
*/
|
|
public function contact()
|
|
{
|
|
return $this->belongsTo(Contact::class);
|
|
}
|
|
|
|
/**
|
|
* Limit tasks to completed ones.
|
|
*
|
|
* @param Builder $query
|
|
* @return Builder
|
|
*/
|
|
public function scopeCompleted(Builder $query)
|
|
{
|
|
return $query->where('status', 'completed');
|
|
}
|
|
|
|
/**
|
|
* Limit tasks to in-progress ones.
|
|
*
|
|
* @param Builder $query
|
|
* @return Builder
|
|
*/
|
|
public function scopeInProgress(Builder $query)
|
|
{
|
|
return $query->where('status', 'inprogress');
|
|
}
|
|
|
|
/**
|
|
* Toggle task status.
|
|
*
|
|
* @return static
|
|
*/
|
|
public function toggle()
|
|
{
|
|
$this->status = $this->status === 'completed' ? 'inprogress' : 'completed';
|
|
$this->save();
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getTitle()
|
|
{
|
|
return $this->title;
|
|
}
|
|
|
|
public function getDescription()
|
|
{
|
|
return $this->description;
|
|
}
|
|
|
|
public function getCreatedAt()
|
|
{
|
|
return $this->created_at;
|
|
}
|
|
}
|