@@ 12-68 (lines=57) @@ | ||
9 | use GeneaLabs\LaravelModelCaching\CachedBuilder; |
|
10 | use Illuminate\Database\Eloquent\Relations\HasManyThrough; |
|
11 | ||
12 | 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 |
@@ 11-66 (lines=56) @@ | ||
8 | use Illuminate\Database\Eloquent\Relations\HasOneThrough; |
|
9 | use Illuminate\Database\Eloquent\SoftDeletes; |
|
10 | ||
11 | class UncachedAuthorWithInlineGlobalScope extends Model |
|
12 | { |
|
13 | use SoftDeletes; |
|
14 | ||
15 | protected $casts = [ |
|
16 | "finances" => "array", |
|
17 | ]; |
|
18 | protected $fillable = [ |
|
19 | 'name', |
|
20 | 'email', |
|
21 | "finances", |
|
22 | ]; |
|
23 | protected $table = "authors"; |
|
24 | ||
25 | protected static function boot() |
|
26 | { |
|
27 | parent::boot(); |
|
28 | ||
29 | static::addGlobalScope('inlineScope', function (Builder $builder) { |
|
30 | return $builder->where('name', 'LIKE', "A%"); |
|
31 | }); |
|
32 | } |
|
33 | ||
34 | public function books() : HasMany |
|
35 | { |
|
36 | return $this->hasMany(UncachedBook::class); |
|
37 | } |
|
38 | ||
39 | public function printers() : HasManyThrough |
|
40 | { |
|
41 | return $this->hasManyThrough(Printer::class, Book::class, "author_id"); |
|
42 | } |
|
43 | ||
44 | public function profile() : HasOne |
|
45 | { |
|
46 | return $this->hasOne(UncachedProfile::class); |
|
47 | } |
|
48 | ||
49 | public function getLatestBookAttribute() |
|
50 | { |
|
51 | return $this |
|
52 | ->books() |
|
53 | ->latest("id") |
|
54 | ->first(); |
|
55 | } |
|
56 | ||
57 | public function scopeStartsWithA(Builder $query) : Builder |
|
58 | { |
|
59 | return $query->where('name', 'LIKE', 'A%'); |
|
60 | } |
|
61 | ||
62 | public function scopeNameStartsWith(Builder $query, string $startOfName) : Builder |
|
63 | { |
|
64 | return $query->where("name", "LIKE", "{$startOfName}%"); |
|
65 | } |
|
66 | } |
|
67 |