Completed
Pull Request — master (#372)
by Mike
31:01 queued 16:07
created

Author::scopeStartsWithA()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2
3
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4
use Illuminate\Database\Eloquent\Builder;
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\HasMany;
7
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
8
use Illuminate\Database\Eloquent\Relations\HasOne;
9
use Illuminate\Database\Eloquent\SoftDeletes;
10
11
class Author extends Model
12
{
13
    use Cachable;
14
    use SoftDeletes;
15
16
    protected $casts = [
17
        "finances" => "array",
18
    ];
19
    protected $fillable = [
20
        'name',
21
        'email',
22
        "finances",
23
    ];
24
25
    public function books() : HasMany
26
    {
27
        return $this->hasMany(Book::class);
28
    }
29
30
    public function printers() : HasManyThrough
31
    {
32
        return $this->hasManyThrough(Printer::class, Book::class);
33
    }
34
    
35
    public function profile() : HasOne
36
    {
37
        return $this->hasOne(Profile::class);
38
    }
39
40
    public function getLatestBookAttribute()
41
    {
42
        return $this
43
            ->books()
44
            ->latest("id")
45
            ->first();
46
    }
47
48
    public function scopeStartsWithA(Builder $query) : Builder
49
    {
50
        return $query->where('name', 'LIKE', 'A%');
51
    }
52
53
    public function scopeNameStartsWith(Builder $query, string $startOfName) : Builder
54
    {
55
        return $query->where("name", "LIKE", "{$startOfName}%");
56
    }
57
58
    public function scopeBooksStartWith(Builder $query, string $startOfName) : Builder
59
    {
60
        return $query->where("name", "LIKE", "{$startOfName}%");
61
    }
62
}
63