1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chriscreate\Blog\Traits\Post; |
4
|
|
|
|
5
|
|
|
use Chriscreate\Blog\Category; |
6
|
|
|
use Chriscreate\Blog\Post; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
|
10
|
|
|
trait PostCategoryScopes |
11
|
|
|
{ |
12
|
|
|
public function scopeWhereCategories(Builder $query, $categories = null) |
13
|
|
|
{ |
14
|
|
|
// search by category name |
15
|
|
|
if (is_string($categories)) { |
16
|
|
|
return $this->whereCategory('name', $categories); |
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
// search by category id |
20
|
|
|
if (is_int($categories)) { |
21
|
|
|
return $this->whereCategory('id', $categories); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
// search by multiple categories |
25
|
|
|
if (is_array($categories)) { |
26
|
|
|
if (is_int($categories[0])) { |
27
|
|
|
$field = 'id'; |
28
|
|
|
} else { |
29
|
|
|
$field = 'name'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $this->whereCategory($field, $categories); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// search by category model |
36
|
|
|
if ($categories instanceof Category) { |
37
|
|
|
return $this->whereCategory('id', $categories->id); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// search by categories collection |
41
|
|
|
if ($categories instanceof Collection) { |
42
|
|
|
return $this->whereCategory('id', $categories->pluck('id')->toArray()); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $query; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function scopeWhereCategory(Builder $query, ...$options) |
49
|
|
|
{ |
50
|
|
|
$collection = collect([ |
51
|
|
|
'field' => 'id', |
52
|
|
|
'operator' => '=', |
53
|
|
|
'value' => null, |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
// Search by field and value |
57
|
|
View Code Duplication |
if (count($options) == 2) { |
|
|
|
|
58
|
|
|
$collection = $collection->replace(['field' => $options[0]]) |
59
|
|
|
->replace(['value' => $options[1]]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Search by field, operator and value |
63
|
|
View Code Duplication |
if (count($options) == 3) { |
|
|
|
|
64
|
|
|
$collection = $collection->replace(['field' => $options[0]]) |
65
|
|
|
->replace(['operator' => $options[1]]) |
66
|
|
|
->replace(['value' => $options[2]]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$query->with('category'); |
70
|
|
|
|
71
|
|
|
if (is_array($collection['value'])) { |
72
|
|
|
return $query->whereHas( |
73
|
|
|
'category', |
74
|
|
|
function (Builder $query) use ($collection) { |
75
|
|
|
return $query->whereIn( |
76
|
|
|
$collection['field'], |
77
|
|
|
$collection['value'] |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $query->whereHas( |
84
|
|
|
'category', |
85
|
|
|
function (Builder $query) use ($collection) { |
86
|
|
|
return $query->where( |
87
|
|
|
$collection['field'], |
88
|
|
|
$collection['operator'], |
89
|
|
|
$collection['value'] |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function scopeRelatedByPostCategory(Builder $query, Post $post) |
96
|
|
|
{ |
97
|
|
|
return $query->whereHas('category', function (Builder $query) use ($post) { |
98
|
|
|
return $query->where('id', $post->category->id); |
99
|
|
|
})->where('id', '!=', $post->id); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.