家庭任務平台中主要使用了使用者,計畫,任務,活動,個人資料,這五者的關係分別為
今天是還沒有介紹過的Model,分別為Task、Activity、Profile、MemberProject
class Task extends Model
{
use Recordability;
protected $fillable = ['body', 'completed','start','due','assignee_id'];
protected $touches = ['project'];
protected $casts = [
'completed' => 'boolean',
'start'=>'datetime:Y-m-d H:i:s',
'due'=>'datetime:Y-m-d H:i:s'
];
public function project()
{
return $this->belongsTo('App\Project');
}
public function assignee()
{
return $this->belongsTo('App\User','assignee_id');
}
public function path()
{
return '/projects/' . $this->project->id . '/tasks/' . $this->id;
}
public function activities()
{
return $this->morphMany('App\Activity', 'recordable');
}
}
class Activity extends Model
{
protected $fillable=['description','changes','user_id'];
protected $casts = ['changes' => 'array'];
public function recordable()
{
return $this->morphTo();
}
public function user(){
return $this->belongsTo('App\User');
}
}
class Profile extends Model
{
protected $fillable=['user_id','avatar'];
public function user()
{
return $this->belongsTo('App\User');
}
}
class MemberProject extends Model
{
protected $table = 'member_project';
protected $fillable =['member_id','project_id'];
}
在Eloquent中提供很多方式將資料表連結在一起,像常見的belongsTo<->hasMany等一對多等關係,因此接下來整理這些關係的連結方法。