*/ protected $fillable = [ 'portrait', 'name', 'title', 'positions', 'text', 'sort', 'member_type', 'status', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'sort' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /** * The attributes that should be hidden for serialization. * * @var array */ protected $hidden = []; /** * Default values for attributes. * * @var array */ protected $attributes = [ 'sort' => 999, 'status' => 'draft', ]; /** * Scope a query to only include members of a given type. * * @param \Illuminate\Database\Eloquent\Builder $query * @param string $type * @return \Illuminate\Database\Eloquent\Builder */ public function scopeOfType($query, $type) { return $query->where('member_type', $type); } /** * Scope a query to only include board members. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeBoard($query) { return $query->where('member_type', 'board'); } /** * Scope a query to only include executive members. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeExecutive($query) { return $query->where('member_type', 'executive'); } /** * Scope a query to only include published members. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopePublished($query) { return $query->where('status', 'publish'); } /** * Scope a query to only include draft members. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeDraft($query) { return $query->where('status', 'draft'); } /** * Scope a query to order members by sort field (ascending). * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeOrdered($query) { return $query->orderBy('sort', 'asc')->orderBy('name', 'asc'); } /** * Get the positions as an array. * * @return array */ public function getPositionsArrayAttribute() { if (empty($this->positions)) { return []; } // Split by \r\n or \n return array_filter( preg_split('/\r\n|\n|\r/', $this->positions), function ($position) { return !empty(trim($position)); } ); } /** * Check if member is published. * * @return bool */ public function isPublished() { return $this->status === 'publish'; } /** * Check if member is a board member. * * @return bool */ public function isBoard() { return $this->member_type === 'board'; } /** * Check if member is an executive member. * * @return bool */ public function isExecutive() { return $this->member_type === 'executive'; } }