Select condition Laravel Hello guys,
I have two select lists. First list A store data with resources, second list B store data with buildings.
Resources is in relationship with Building 1:N one to many with resource_id (in buildings table). And I need to do condition like this: If selected option in list A show select list B sorted by resource_id.
For this moment I have two select list:
<div class="form-group">
<label for="resource_id">Resource:</label>
<select class="form-control" name="resource_id">
@foreach($resources as $resource)
<option value="{{ $resource->id }}">{{ $resource->display_name }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="building_id">Building:</label>
<select class="form-control" name="building_id">
@foreach($buildings as $building)
<option value="{{ $building->id }}">{{ $building->display_name }}</option>
@endforeach
</select>
</div>
Now "building" select have all buildings, how to filter them by resource_id?
My building table looks like:
ID
Display_name
Resource_id (foreign key)
Controller:
$resources = Resource::all();
$buildings = Building::all();
Thanks so much for any help.
$resources = Resource::all();
$buildings = Building::whereIn('resource_id ', $resources->pluck('id'))->orderBy('resource_id')->get();
@MohamedTammam Column not found: 1054 Unknown column 'resource_id ' in 'where clause' (SQL: select * from buildings where resource_id in (1, 2, 3) and buildings.deleted_at is null order by resource_id asc)
I have resource_id column in buildings table
@ziben69 Sorry, I was adding an extra space.
I updated my answer, Please try now.
@MohamedTammam ok now no error but it still display all of my buildings in select list. It should be conditionet by first select list. If user select resource from:
<select class="form-control" name="resource_id">
@foreach($resources as $resource)
<option value="{{ $resource->id }}">{{ $resource->display_name }}</option>
@endforeach
</select>
where ID = 2 second select should show only buildings where resource_id = 2
@ziben69 You need to do an AJAX request for this or store all the buildings for every resource in a JS object and change it dynamically.
This is most better. Display resource with old resorce selected.
<select class="form-control" name="resource_id">
@foreach($resources as $resource)
<option value="{{ $resource->id }}"
{{ old('resource_id', $model->resource_id) == $resource->id ? 'selected' : ' ' }}>
{{ $resource->display_name }}
</option>
@endforeach
</select>
@ziben69
What is the model that belongs to a resource
with resource_id? The model is this.
In order to recover the old resource and make the select be reused.
$buildings = Building::has('resources')->get();
I did something like this with jQuery:
Blade:
<div class="form-group">
<select class="form-control" name="resource_id">
@foreach($resources as $resource)
<option value="{{ $resource->id }}">{{ $resource->display_name }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<select class="form-control" name="building_id">
@foreach($buildings as $building)
<option value="{{ $building->id }}">{{ $building->display_name }}</option>
@endforeach
</select>
</div>
<script>
$(function() {
$('select[name=resource_id]').change(function() {
var url = '{{ url('dashboard/usterki/nowa') }}/' + $(this).val();
$.get(url, function(data) {
var select = $('form select[name=building_id]');
select.empty();
$.each(data,function(key, value) {
select.append('<option value=' + value.id + '>' + value.name + '</option>');
});
});
});
});
</script>
DefectController:
public function getBuildings(Resource $resource) {
return $resource->buildings()->select('id', 'display_name')->get();
}
Route:
Route::get('/usterka/nowa/{defect}/', 'DefectController@getBuildings');
i see that filtering by selecting option from Resource select works but it giving me empty result (second select - named building_id).
What I do wrong?
Please sign in or create an account to participate in this conversation.