belongsTo(Character::class); } /** * Get the raid that's associated with this card. */ public function raid() { return $this->belongsTo(Raid::class); } /** * Who, Where or What this card belongs to. */ public function getSubjectAttribute() { if ($this->character) { return $this->character->name; } else if ($this->class) { return Str::ucfirst($this->class); } else if ($this->role) { return Str::ucfirst($this->role); } return "Somebody"; } /** * Who, Where or What this card belongs to. */ public function getSubjectTypeAttribute() { if ($this->character) { return "Character"; } else if ($this->class) { return "Class"; } else if ($this->role) { return "Roll"; } return "Anyone"; } /** * Get cards depending on settings. */ public static function getBySettings(GameSettings $settings, ?int $max = null) { // Map type of setting to database column. $type_map = [ 'characters' => 'character_id', 'raids' => 'raid_id', 'classes' => 'class', 'roles' => 'role' ]; $query = self::query() ->inRandomOrder(); // Run through all settings and apply filter. foreach ($settings->all() as $type => $values) { $column = $type_map[$type]; $query->where(function ($q) use ($column, $values) { $q->whereIn($column, $values)->orWhereNull($column); }); } if ($max !== null) { $query->limit($max); } return $query->get() ->makeHidden(['character', 'class', 'deleted_at']); } }