diff options
author | Michael Weimann <mail@michael-weimann.eu> | 2019-12-03 20:09:37 +0100 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2019-12-07 21:29:11 +0100 |
commit | 4f63bbbaacd0db3636f1169f5ad29f1cfb21615b (patch) | |
tree | ec32a00731a1f3648ca1b422434bbb0e035e61b2 /src | |
parent | 24578c5cb0f75ae6ad0f52577f9fcb617ae87624 (diff) |
Add Question model
Diffstat (limited to 'src')
-rw-r--r-- | src/Models/Question.php | 64 | ||||
-rw-r--r-- | src/Models/User/User.php | 22 |
2 files changed, 86 insertions, 0 deletions
diff --git a/src/Models/Question.php b/src/Models/Question.php new file mode 100644 index 00000000..eccf7fe6 --- /dev/null +++ b/src/Models/Question.php @@ -0,0 +1,64 @@ +<?php + +declare(strict_types=1); + +namespace Engelsystem\Models; + +use Engelsystem\Models\User\User; +use Engelsystem\Models\User\UsesUserModel; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Relations\BelongsTo; + +/** + * @property integer $id + * @property string $text + * @property string $answer + * @property integer $answerer_id + * @property-read User $answerer + * @method static Builder|Question whereAnswer($value) + * @method static Builder|Question whereAnswererId($value) + * @method static Builder|Question whereId($value) + * @method static Builder|Question whereQuestion($value) + */ +class Question extends BaseModel +{ + use UsesUserModel; + + /** @var string[] */ + protected $fillable = [ + 'user_id', + 'text', + 'answerer_id', + 'answer', + ]; + + /** @var string[] */ + protected $casts = [ + 'user_id' => 'integer', + 'answerer_id' => 'integer', + ]; + + /** + * @return BelongsTo + */ + public function answerer(): BelongsTo + { + return $this->belongsTo(User::class, 'answerer_id'); + } + + /** + * @return Builder + */ + public static function unanswered(): Builder + { + return static::whereAnswererId(null); + } + + /** + * @return Builder + */ + public static function answered(): Builder + { + return static::whereNotNull('answerer_id'); + } +} diff --git a/src/Models/User/User.php b/src/Models/User/User.php index 11d88505..e2ee9b21 100644 --- a/src/Models/User/User.php +++ b/src/Models/User/User.php @@ -6,6 +6,7 @@ use Carbon\Carbon; use Engelsystem\Models\BaseModel; use Engelsystem\Models\News; use Engelsystem\Models\NewsComment; +use Engelsystem\Models\Question; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasOne; @@ -38,6 +39,9 @@ use Illuminate\Database\Query\Builder as QueryBuilder; * @method static QueryBuilder|User[] whereLastLoginAt($value) * @method static QueryBuilder|User[] whereCreatedAt($value) * @method static QueryBuilder|User[] whereUpdatedAt($value) + * + * @property-read Collection|Question[] $questionsAsked + * @property-read Collection|Question[] $questionsAnswered */ class User extends BaseModel { @@ -119,4 +123,22 @@ class User extends BaseModel { return $this->hasMany(NewsComment::class); } + + /** + * @return HasMany + */ + public function questionsAsked(): HasMany + { + return $this->hasMany(Question::class, 'user_id') + ->where('user_id', $this->id); + } + + /** + * @return HasMany + */ + public function questionsAnswered(): HasMany + { + return $this->hasMany(Question::class, 'answerer_id') + ->where('answerer_id', $this->id); + } } |