|
2 | 2 |
|
3 | 3 | namespace Statamic\Eloquent\Taxonomies; |
4 | 4 |
|
5 | | -use Illuminate\Support\Arr; |
6 | 5 | use Illuminate\Support\Str; |
7 | 6 | use Statamic\Contracts\Taxonomies\Term as TermContract; |
8 | 7 | use Statamic\Facades\Blink; |
@@ -215,46 +214,34 @@ private function applyCollectionAndTaxonomyWheres() |
215 | 214 | ? Taxonomy::handles()->all() |
216 | 215 | : $this->taxonomies; |
217 | 216 |
|
218 | | - // get entries in each collection that have a value for the taxonomies we are querying |
219 | | - // or the ones associated with the collection |
220 | | - // what we ultimately want is a subquery for terms in the form: |
221 | | - // where('taxonomy', $taxonomy)->whereIn('slug', $slugArray) |
222 | | - $collectionTaxonomyHash = md5(collect($this->collections)->merge(collect($taxonomies))->sort()->join('-')); |
223 | | - |
224 | | - Blink::once("eloquent-taxonomy-hash-{$collectionTaxonomyHash}", function () use ($taxonomies) { |
225 | | - return Entry::query() |
226 | | - ->whereIn('collection', $this->collections) |
227 | | - ->select($taxonomies) |
228 | | - ->get(); |
229 | | - }) |
230 | | - ->flatMap(function ($entry) use ($taxonomies) { |
231 | | - $slugs = []; |
232 | | - foreach ($entry->collection()->taxonomies()->map->handle() as $taxonomy) { |
233 | | - if (in_array($taxonomy, $taxonomies)) { |
234 | | - foreach (Arr::wrap($entry->get($taxonomy, [])) as $term) { |
235 | | - $slugs[] = $taxonomy.'::'.$term; |
236 | | - } |
237 | | - } |
| 217 | + collect($taxonomies)->each(function ($taxonomy) use ($query) { |
| 218 | + $collectionTaxonomyHash = md5(collect($this->collections)->merge([$taxonomy])->sort()->join('-')); |
| 219 | + |
| 220 | + $terms = Blink::once("eloquent-taxonomy-hash-{$collectionTaxonomyHash}", function () use ($taxonomy) { |
| 221 | + if (! $taxonomy = Taxonomy::find($taxonomy)) { |
| 222 | + return []; |
238 | 223 | } |
239 | 224 |
|
240 | | - return $slugs; |
241 | | - }) |
242 | | - ->unique() |
243 | | - ->map(function ($term) { |
244 | | - return [ |
245 | | - 'taxonomy' => Str::before($term, '::'), |
246 | | - 'term' => Str::after($term, '::'), |
247 | | - ]; |
248 | | - }) |
249 | | - ->mapToGroups(function ($item) { |
250 | | - return [$item['taxonomy'] => $item['term']]; |
251 | | - }) |
252 | | - ->each(function ($terms, $taxonomy) use ($query) { |
| 225 | + return TermModel::where('taxonomy', $taxonomy) |
| 226 | + ->select('slug') |
| 227 | + ->get() |
| 228 | + ->map(function ($term) use ($taxonomy) { |
| 229 | + return Entry::query() |
| 230 | + ->whereIn('collection', $this->collections) |
| 231 | + ->whereJsonContains('data->'.$taxonomy, [$term->slug]) |
| 232 | + ->count() > 0 ? $term->slug : null; |
| 233 | + }) |
| 234 | + ->filter() |
| 235 | + ->values(); |
| 236 | + }); |
| 237 | + |
| 238 | + if ($terms->isNotEmpty()) { |
253 | 239 | $query->orWhere(function ($query) use ($terms, $taxonomy) { |
254 | 240 | $query->where('taxonomy', $taxonomy) |
255 | | - ->whereIn('slug', $terms); |
| 241 | + ->whereIn('slug', $terms->all()); |
256 | 242 | }); |
257 | | - }); |
| 243 | + } |
| 244 | + }); |
258 | 245 | }); |
259 | 246 | } |
260 | 247 |
|
|
0 commit comments