Closed wm-simon closed 1 year ago
Can you better explain what selected:services
is supposed to be?
I don't understand this:
From livewire I get the selected services:
selected:services - be6439fd-ed8e-4928-8ada-69cecc670a8b - e79d1c7b-913f-49f4-ab2e-e3d603856fca
It's a nested array:
selected
- services // collection entries
- be6439fd-ed8e-4928-8ada-69cecc670a8b
- e79d1c7b-913f-49f4-ab2e-e3d603856fca
- branches: // taxonomy entries
- gastronomie
I've tried it with a not nested array selected_services
as well, but there was the same error.
selected_services
- be6439fd-ed8e-4928-8ada-69cecc670a8b
- e79d1c7b-913f-49f4-ab2e-e3d603856fca
What you're looking for is an intersect, which is supported on the query builder level with whereJsonContains
. However there's currently not a collection tag parameter equivalent.
You can use a custom query scope at the moment. Here's one:
<?php
namespace App\Scopes;
use Statamic\Query\Scopes\Scope;
class Intersect extends Scope
{
public function apply($query, $values)
{
$query->whereJsonContains(
$values->get('intersect_field'),
$values->explode('intersect_value'),
);
}
}
Put that in app/Scopes/Intersect.php
then use it like this:
{{ collection from="references" query_scope="intersect" intersect_field="services" :intersect_value="selected" }}
...
{{ /collection }}
Notice that :intersect_value
has a :
prefix.
Thanks for your help. I'm not sure, if I'm doing right.
That's my tag right now:
{{ collection
from="references"
not_listed:is="false"
query_scope="intersect"
intersect_field="services"
intersect_value="{selected:services}"
taxonomy:branchen="{selected:branches}"
sort="{sort}"
}}
But now there are no entries shown anymore, when selected:services
is empty.
When I select a service, there is shown this error:
explode(): Argument #2 ($string) must be of type string, array given
If it helps, I use the checkboxes like it is explained here: https://laravel-livewire.com/screencasts/form-checkboxes
Thats's my code:
Generates the checkpoints:
{{ collection:leistungen }}
<input type="checkbox" value="{{ id }}" wire:model="selected.services">
{{ /collection:leistungen }}
Livewire component:
<?php
namespace App\Http\Livewire;
use Livewire\Component;
class FilterCollection extends Component
{
public $selected = [
'services' => [],
'branches' => [],
];
public $sort = "order:desc";
public function render()
{
return view('livewire.filter-collection');
}
}
Hey @jasonvarga , I'm not sure, if you get a notification, when the issue was closed before. Is it possible for you to take a look on it again?
Looks like the scope is expecting a string but the data is an array. Try changing the scope to not explode (that expects a string and turns it into an array), as you already have an array.
Hey, could we check this again, perhaps I'm missing something.
I have 2 arrays with entry ids. With them I want to check, if both arrays contain the same ids.
I've added to the intersect.
echo '<pre>';
var_dump($values->get('intersect_field'));
var_dump($values->get('intersect_value'));
echo '</pre>';
This is the output:
string(8) "services"
array(3) {
["services"]=>
array(0) {
}
["branches"]=>
array(0) {
}
["date"]=>
string(0) ""
}
But string(8) "services"
should be the ids from the the services
field inside the collection entries, right?
For testing I changed the intersect to this:
$values->get('intersect_field'),
$values->get('intersect_value'),
If there is nothing selected, there are no entries shown. When I select a service, it loads endless.
Bug description
I've tried to filter a references collection with livewire. I get this error:
foreach() argument must be of type array|object, null given
How to reproduce
The references entries have a field with related services entries:
From livewire I get the selected services:
Now I want to show the entries with this tag:
{{ collection from="references" not_listed:is="false" taxonomy:branchen="{selected:branches}" services:in="{selected:services|option_list}" }}
I've tried multiple versions, but nothing worked:
This error is shown:
foreach() argument must be of type array|object, null given
I think this should work, like it is explained in the docs: https://statamic.dev/conditions#passing-multiple-values
The filter for branches with taxonomies working fine without an error.
Logs
No response
Environment
Installation
Fresh statamic/statamic site via CLI
Antlers Parser
runtime (new)
Additional details
No response