Skip to content

Commit 23a0630

Browse files
authored
Merge pull request #5650 from Laravel-Backpack/checklist-dependency-option-filter
allow checklist dependency to filter options
2 parents 1eaebf7 + ce16da4 commit 23a0630

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

src/resources/views/crud/fields/checklist_dependency.blade.php

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,25 @@
99

1010
@include('crud::fields.inc.wrapper_start')
1111

12-
<label>{!! $field['label'] !!}</label>
13-
<?php
14-
$entity_model = $crud->getModel();
12+
<label>{!! $field['label'] !!}</label>
13+
<?php
14+
$entity_model = $crud->getModel();
1515
1616
//short name for dependency fields
1717
$primary_dependency = $field['subfields']['primary'];
1818
$secondary_dependency = $field['subfields']['secondary'];
1919
2020
//all items with relation
21-
$dependencies = $primary_dependency['model']::with($primary_dependency['entity_secondary'])->get();
21+
$dependencies = $primary_dependency['model']::with($primary_dependency['entity_secondary']);
22+
23+
if(isset($primary_dependency['options']) && $primary_dependency['options'] instanceof \Closure){
24+
$dependencies = $primary_dependency['options']($dependencies);
25+
}
26+
27+
// check if dependencies are a query builder, or the developer already called `->get()` on it.
28+
if ($dependencies instanceof \Illuminate\Contracts\Database\Query\Builder) {
29+
$dependencies = $dependencies->get();
30+
}
2231
2332
$dependencyArray = [];
2433
@@ -64,6 +73,22 @@
6473
6574
//json encode of dependency matrix
6675
$dependencyJson = json_encode($dependencyArray);
76+
77+
$primaryDependencyOptionQuery = $primary_dependency['model']::query();
78+
79+
if(isset($primary_dependency['options']) && $primary_dependency['options'] instanceof \Closure){
80+
$primaryDependencyOptionQuery = $primary_dependency['options']($primaryDependencyOptionQuery);
81+
}
82+
83+
$primaryDependencyOptions = $primaryDependencyOptionQuery->get();
84+
85+
$secondaryDependencyOptionQuery = $secondary_dependency['model']::query();
86+
87+
if(isset($secondary_dependency['options']) && $secondary_dependency['options'] instanceof \Closure){
88+
$secondaryDependencyOptionQuery = $secondary_dependency['options']($secondaryDependencyOptionQuery);
89+
}
90+
91+
$secondaryDependencyOptions = $secondaryDependencyOptionQuery->get();
6792
?>
6893

6994
<div class="container">
@@ -92,7 +117,7 @@
92117
@endif
93118
</div>
94119

95-
@foreach ($primary_dependency['model']::all() as $connected_entity_entry)
120+
@foreach ($primaryDependencyOptions as $connected_entity_entry)
96121
<div class="col-sm-{{ isset($primary_dependency['number_columns']) ? intval(12/$primary_dependency['number_columns']) : '4'}}">
97122
<div class="checkbox">
98123
<label class="font-weight-normal">
@@ -103,7 +128,7 @@ class = 'primary_list'
103128
@if (is_string($attribute) && $attribute != 'value')
104129
@if ($attribute=='name')
105130
{{ $attribute }}="{{ $value }}_show[]"
106-
@else
131+
@elseif(! $value instanceof \Closure)
107132
{{ $attribute }}="{{ $value }}"
108133
@endif
109134
@endif
@@ -143,28 +168,28 @@ class = 'primary_list'
143168
@endif
144169
</div>
145170

146-
@foreach ($secondary_dependency['model']::all() as $connected_entity_entry)
171+
@foreach ($secondaryDependencyOptions as $connected_entity_entry)
147172
<div class="col-sm-{{ isset($secondary_dependency['number_columns']) ? intval(12/$secondary_dependency['number_columns']) : '4'}}">
148173
<div class="checkbox">
149174
<label class="font-weight-normal">
150175
<input type="checkbox"
151-
class = 'secondary_list'
152-
data-id = "{{ $connected_entity_entry->id }}"
176+
class="secondary_list"
177+
data-id="{{ $connected_entity_entry->id }}"
153178
@foreach ($secondary_dependency as $attribute => $value)
154179
@if (is_string($attribute) && $attribute != 'value')
155180
@if ($attribute=='name')
156181
{{ $attribute }}="{{ $value }}_show[]"
157-
@else
182+
@elseif(! $value instanceof \Closure)
158183
{{ $attribute }}="{{ $value }}"
159184
@endif
160185
@endif
161186
@endforeach
162187
value="{{ $connected_entity_entry->id }}"
163188

164189
@if( ( isset($field['value']) && is_array($field['value']) && ( in_array($connected_entity_entry->id, $field['value'][1]->pluck('id', 'id')->toArray()) || isset( $secondary_ids[$connected_entity_entry->id])) || $old_secondary_dependency && in_array($connected_entity_entry->id, $old_secondary_dependency)))
165-
checked = "checked"
190+
checked="checked"
166191
@if(isset( $secondary_ids[$connected_entity_entry->id]))
167-
disabled = disabled
192+
disabled="disabled"
168193
@endif
169194
@endif > {{ $connected_entity_entry->{$secondary_dependency['attribute']} }}
170195
</label>

0 commit comments

Comments
 (0)