1: | <?php |
2: | |
3: | namespace Baril\Orderly\Concerns; |
4: | |
5: | use Baril\Orderly\Relations\BelongsToManyOrderable; |
6: | use Baril\Orderly\Relations\MorphToManyOrderable; |
7: | use Illuminate\Support\Arr; |
8: | use Illuminate\Support\Str; |
9: | |
10: | trait HasOrderableRelationships |
11: | { |
12: | public static function bootHasOrderableRelationships() |
13: | { |
14: | static::$manyMethods = array_merge(static::$manyMethods, [ |
15: | 'belongsToManyOrderable', |
16: | 'morphToManyOrderable', |
17: | 'morphedByManyOrderable', |
18: | 'belongsToManyOrdered', |
19: | 'morphToManyOrdered', |
20: | 'morphedByManyOrdered', |
21: | ]); |
22: | } |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | public function belongsToManyOrderable( |
41: | $related, |
42: | $orderColumn = 'position', |
43: | $table = null, |
44: | $foreignPivotKey = null, |
45: | $relatedPivotKey = null, |
46: | $parentKey = null, |
47: | $relatedKey = null, |
48: | $relation = null |
49: | ) { |
50: | |
51: | |
52: | |
53: | |
54: | if (is_null($relation)) { |
55: | $relation = $this->guessBelongsToManyRelation(); |
56: | } |
57: | |
58: | |
59: | |
60: | |
61: | $instance = $this->newRelatedInstance($related); |
62: | |
63: | $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey(); |
64: | |
65: | $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey(); |
66: | |
67: | |
68: | |
69: | |
70: | if (is_null($table)) { |
71: | $table = $this->joiningTable($related); |
72: | } |
73: | |
74: | return new BelongsToManyOrderable( |
75: | $instance->newQuery(), |
76: | $this, |
77: | $orderColumn, |
78: | $table, |
79: | $foreignPivotKey, |
80: | $relatedPivotKey, |
81: | $parentKey ?: $this->getKeyName(), |
82: | $relatedKey ?: $instance->getKeyName(), |
83: | $relation |
84: | ); |
85: | } |
86: | |
87: | public function belongsToManyOrdered( |
88: | $related, |
89: | $orderColumn = 'position', |
90: | $table = null, |
91: | $foreignPivotKey = null, |
92: | $relatedPivotKey = null, |
93: | $parentKey = null, |
94: | $relatedKey = null, |
95: | $relation = null |
96: | ) { |
97: | |
98: | return $this->belongsToManyOrderable( |
99: | $related, |
100: | $orderColumn, |
101: | $table, |
102: | $foreignPivotKey, |
103: | $relatedPivotKey, |
104: | $parentKey, |
105: | $relatedKey, |
106: | $relation |
107: | )->ordered(); |
108: | } |
109: | |
110: | |
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | public function morphToManyOrderable( |
124: | $related, |
125: | $name, |
126: | $orderColumn = 'position', |
127: | $table = null, |
128: | $foreignPivotKey = null, |
129: | $relatedPivotKey = null, |
130: | $parentKey = null, |
131: | $relatedKey = null, |
132: | $inverse = false |
133: | ) { |
134: | |
135: | $caller = $this->guessBelongsToManyRelation(); |
136: | |
137: | |
138: | |
139: | |
140: | $instance = $this->newRelatedInstance($related); |
141: | |
142: | $foreignPivotKey = $foreignPivotKey ?: $name . '_id'; |
143: | |
144: | $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey(); |
145: | |
146: | |
147: | |
148: | |
149: | $table = $table ?: Str::plural($name); |
150: | |
151: | return new MorphToManyOrderable( |
152: | $instance->newQuery(), |
153: | $this, |
154: | $name, |
155: | $orderColumn, |
156: | $table, |
157: | $foreignPivotKey, |
158: | $relatedPivotKey, |
159: | $parentKey ?: $this->getKeyName(), |
160: | $relatedKey ?: $instance->getKeyName(), |
161: | $caller, |
162: | $inverse |
163: | ); |
164: | } |
165: | |
166: | public function morphToManyOrdered( |
167: | $related, |
168: | $name, |
169: | $orderColumn = 'position', |
170: | $table = null, |
171: | $foreignPivotKey = null, |
172: | $relatedPivotKey = null, |
173: | $parentKey = null, |
174: | $relatedKey = null, |
175: | $inverse = false |
176: | ) { |
177: | |
178: | return $this->morphToManyOrderable( |
179: | $related, |
180: | $name, |
181: | $orderColumn, |
182: | $table, |
183: | $foreignPivotKey, |
184: | $relatedPivotKey, |
185: | $parentKey, |
186: | $relatedKey, |
187: | $inverse |
188: | )->ordered(); |
189: | } |
190: | |
191: | |
192: | |
193: | |
194: | |
195: | |
196: | |
197: | |
198: | |
199: | |
200: | |
201: | |
202: | |
203: | |
204: | |
205: | |
206: | public function morphedByManyOrderable( |
207: | $related, |
208: | $name, |
209: | $orderColumn = 'position', |
210: | $table = null, |
211: | $foreignPivotKey = null, |
212: | $relatedPivotKey = null, |
213: | $parentKey = null, |
214: | $relatedKey = null |
215: | ) { |
216: | |
217: | $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey(); |
218: | |
219: | |
220: | |
221: | |
222: | $relatedPivotKey = $relatedPivotKey ?: $name . '_id'; |
223: | |
224: | return $this->morphToManyOrderable( |
225: | $related, |
226: | $name, |
227: | $orderColumn, |
228: | $table, |
229: | $foreignPivotKey, |
230: | $relatedPivotKey, |
231: | $parentKey, |
232: | $relatedKey, |
233: | true |
234: | ); |
235: | } |
236: | |
237: | public function morphedByManyOrdered( |
238: | $related, |
239: | $name, |
240: | $orderColumn = 'position', |
241: | $table = null, |
242: | $foreignPivotKey = null, |
243: | $relatedPivotKey = null, |
244: | $parentKey = null, |
245: | $relatedKey = null |
246: | ) { |
247: | |
248: | return $this->morphedByManyOrderable( |
249: | $related, |
250: | $name, |
251: | $orderColumn, |
252: | $table, |
253: | $foreignPivotKey, |
254: | $relatedPivotKey, |
255: | $parentKey, |
256: | $relatedKey |
257: | )->ordered(); |
258: | } |
259: | } |
260: | |