summaryrefslogtreecommitdiff
path: root/src/Models/Question.php
blob: eccf7fe6966eece91cb86287f84d201877c35b19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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');
    }
}