@@ -12,49 +12,85 @@ trait PostScopes
12
12
{
13
13
public function scopeWhereCategories (Builder $ query , $ categories = null )
14
14
{
15
- if (is_null ($ categories )) {
16
- return $ query ;
17
- }
18
-
15
+ // search by category name
19
16
if (is_string ($ categories )) {
20
- return $ this ->whereCategory ($ categories , 'name ' );
17
+ return $ this ->whereCategory ('name ' , $ categories );
21
18
}
22
19
20
+ // search by category id
23
21
if (is_int ($ categories )) {
24
- return $ this ->whereCategory ($ categories , 'id ' );
22
+ return $ this ->whereCategory ('id ' , $ categories );
25
23
}
26
24
25
+ // search by multiple categories
27
26
if (is_array ($ categories )) {
28
- return $ query ->with ('category ' )
29
- ->whereHas ('category ' , function (Builder $ query ) use ($ categories ) {
30
- return $ query ->whereIn ('id ' , $ categories );
31
- });
27
+ if (is_int ($ categories [0 ])) {
28
+ $ field = 'id ' ;
29
+ } else {
30
+ $ field = 'name ' ;
31
+ }
32
+
33
+ return $ this ->whereCategory ($ field , $ categories );
32
34
}
33
35
36
+ // search by category model
34
37
if ($ categories instanceof Category) {
35
- return $ this ->whereCategory ($ categories ->id , ' id ' );
38
+ return $ this ->whereCategory (' id ' , $ categories ->id );
36
39
}
37
40
41
+ // search by categories collection
38
42
if ($ categories instanceof Collection) {
39
- return $ query ->with ('category ' )
40
- ->whereHas ('category ' , function (Builder $ query ) use ($ categories ) {
41
- return $ query ->whereIn (
42
- 'id ' ,
43
- $ categories ->pluck ('id ' )->toArray ()
44
- );
45
- });
43
+ return $ this ->whereCategory ('id ' , $ categories ->pluck ('id ' )->toArray ());
46
44
}
47
45
48
46
return $ query ;
49
47
}
50
48
51
- public function scopeWhereCategory (Builder $ query , $ category , $ type = ' name ' )
49
+ public function scopeWhereCategory (Builder $ query , ... $ options )
52
50
{
51
+ $ collection = collect ([
52
+ 'field ' => 'id ' ,
53
+ 'operator ' => '= ' ,
54
+ 'value ' => null ,
55
+ ]);
56
+
57
+ // Search by field and value
58
+ if (count ($ options ) == 2 ) {
59
+ $ collection = $ collection ->replace (['field ' => $ options [0 ]])
60
+ ->replace (['value ' => $ options [1 ]]);
61
+ }
62
+
63
+ // Search by field, operator and value
64
+ if (count ($ options ) == 3 ) {
65
+ $ collection = $ collection ->replace (['field ' => $ options [0 ]])
66
+ ->replace (['operator ' => $ options [1 ]])
67
+ ->replace (['value ' => $ options [2 ]]);
68
+ }
69
+
53
70
$ query ->with ('category ' );
54
71
55
- return $ query ->whereHas ('category ' , function (Builder $ query ) use ($ type , $ category ) {
56
- return $ query ->where ($ type , $ category );
57
- });
72
+ if (is_array ($ collection ['value ' ])) {
73
+ return $ query ->whereHas (
74
+ 'category ' ,
75
+ function (Builder $ query ) use ($ collection ) {
76
+ return $ query ->whereIn (
77
+ $ collection ['field ' ],
78
+ $ collection ['value ' ]
79
+ );
80
+ }
81
+ );
82
+ }
83
+
84
+ return $ query ->whereHas (
85
+ 'category ' ,
86
+ function (Builder $ query ) use ($ collection ) {
87
+ return $ query ->where (
88
+ $ collection ['field ' ],
89
+ $ collection ['operator ' ],
90
+ $ collection ['value ' ]
91
+ );
92
+ }
93
+ );
58
94
}
59
95
60
96
public function scopeRelatedByPostTags (Builder $ query , Post $ post )
0 commit comments