I'm trying to have a group of 3 select boxes in a form that are dependant from the one above, in the Country -> State -> Municipality way.
I'm following this tutorial that successfully fetches data and populatates the first select box (Country in this case).
The thing is that in that tutorial the data is inside a single table, while in my application it's in multiple tables.
I'm wondering how could I fetch data across those multiple tables that correspond to the id of the selected item in the select box above? But in the LARAVEL way?
My HTML (all based on the tutorial linked above):
{{-- Muestra los estados sacados de la base de datos. --}}
<div id="inputs-estado">
<div class="form-group">
{{-- Estados --}}
<label for="">Estado</label>
<select name="state" id="state" class="form-control dynamic" data-dependant="state">
@foreach ($estados as $estado)
<option value="{{ $estado->state }}">{{ $estado->state }}</option>
@endforeach
</select>
<br>
{{-- Municipio/Delegación --}}
<label for="">Ciudad</label>
<select name="state" id="state" class="form-control dynamic" data-dependant="city">
<option value="">Selecciona la ciudad</option>
</select>
<br>
{{-- Colonia --}}
<label for="">Municipo</label>
<select name="state" id="state" class="form-control dynamic" data-dependant="municipality">
<option value="">Selecciona el municipio</option>
</select>
</div>
</div>
JS:
formDynamic.change(function () {
if ($(this).val() != '') {
let select = $(this).attr('id');
let value = $(this).val();
let dependent = $(this).data('dependent');
let _token = $('input[name="_token"]').val();
$.ajax({
url: "{{ route('postscontroller.fetch') }}",
method: "POST",
data: {
select: select,
value: value,
_token: _token,
dependent: dependent
},
success: function (result) {
$('#' + dependent).html(result);
}
})
}
});
Controller:
public function create()
{
// Toma los estados de la base de datos.
$estados = DB::connection('db_postalcodes')
->table('state')
->groupBy('state')
->get();
// El with hace que se adjunten variables al view.
return view('admin.posts.create')->with('estados', $estados);
}
public function fetch(Request $request)
{
$state_id = DB::connection('db_postalcodes')->table('city')->get();
$select = $request->get('select');
$value = $request->get('value');
$dependent = $request->get('dependent');
$data = DB::connection('db_postalcodes')
->table('city')
->where($select, $state_id->state_id)
->groupBy($dependent)
->get();
$output = '<option value="">Select '.ucfirst($dependent).'</option>';
foreach($data as $row){
$output .= '<option value="'.$row->$dependent.'">'.$row->$dependent.'</option>';
}
echo $output;
}
Routes.php
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'auth'], function () {
Route::get('/', 'AdminController@index')->name('admin');
Route::get('posts', 'PostsController@index')->name('admin.posts.index');
Route::get('posts/create', 'PostsController@create')->name('admin.posts.create');
Route::post('posts/create', 'PostsController@fetch')->name('postscontroller.fetch');
Route::post('posts', 'PostsController@store')->name('admin.posts.store');
});
My tables:
