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\HasOne; |
8
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
9
|
|
|
use GeneaLabs\LaravelModelCaching\CachedBuilder; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough; |
11
|
|
|
|
12
|
|
View Code Duplication |
class AuthorWithInlineGlobalScope extends Model |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
use Cachable; |
15
|
|
|
use SoftDeletes; |
16
|
|
|
|
17
|
|
|
protected $casts = [ |
18
|
|
|
"finances" => "array", |
19
|
|
|
]; |
20
|
|
|
protected $fillable = [ |
21
|
|
|
'name', |
22
|
|
|
'email', |
23
|
|
|
"finances", |
24
|
|
|
]; |
25
|
|
|
protected $table = "authors"; |
26
|
|
|
|
27
|
|
|
protected static function boot() |
28
|
|
|
{ |
29
|
|
|
parent::boot(); |
30
|
|
|
|
31
|
|
|
static::addGlobalScope('inlineScope', function (CachedBuilder $builder) { |
32
|
|
|
return $builder->where('name', 'LIKE', "A%"); |
33
|
|
|
}); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function books() : HasMany |
37
|
|
|
{ |
38
|
|
|
return $this->hasMany(Book::class); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function printers() : HasManyThrough |
42
|
|
|
{ |
43
|
|
|
return $this->hasManyThrough(Printer::class, Book::class); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function profile() : HasOne |
47
|
|
|
{ |
48
|
|
|
return $this->hasOne(Profile::class); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getLatestBookAttribute() |
52
|
|
|
{ |
53
|
|
|
return $this |
54
|
|
|
->books() |
55
|
|
|
->latest("id") |
56
|
|
|
->first(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function scopeStartsWithA(Builder $query) : Builder |
60
|
|
|
{ |
61
|
|
|
return $query->where('name', 'LIKE', 'A%'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function scopeNameStartsWith(Builder $query, string $startOfName) : Builder |
65
|
|
|
{ |
66
|
|
|
return $query->where("name", "LIKE", "{$startOfName}%"); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.