Я имею дело с проблемой, что я не знаю, как отправить данные из зависимого раскрывающегося списка. Я хочу отправить удостоверения личности города, пара и комнаты. Вот мой клинок:
<div class="col-md-4">
<div class="form-group">
<select class="form-control dropdown-menu-arrow" name="city_id" id ="city" required>
<option value="" selected disabled>Miestas</option>
@foreach($cities as $key=> $value)
<option value="{{$key}}">{{($value)}}</option>
@endforeach
</select>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<select class="form-control dropdown-menu-arrow" name="steam_id" id="steam" required>
<option value="" selected disabled>Steam centras</option>
</select>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<div class="form-group">
<select class="form-control dropdown-menu-arrow" name="room" id="room" required>
<option value="" selected disabled>Kambarys </option>
</select>
</div>
</div>
</div>
</div>
Контроллер:
public function __construct()
{
$this->middleware('auth');
}
public function index(){
$steams = SteamCenter::all();
$courses = Course::all();
$cities = City::all()->pluck("name","id");
return view('sukurti-paskaita',compact('cities'),['courses'=>$courses,'steams'=>$steams]);
}
public function findSteamCenter ($id)
{
$steams= SteamCenter::all()->where("city_id",$id)->pluck("name","id");
return json_encode($steams);
}
public function findRoom($id)
{
$rooms = Room::all()->where("steam_id",$id)->pluck("room_number","id");
return json_encode($rooms);
}
public function insert (Request $request){
$event = Event::create([
'name' => $request->name,
'course_id' => $request->course_id,
'room_id' => $request->room_id,
// 'city' => $request->city_id,
// 'steam' => $request->steam_id,
'description' => $request->description]);
return redirect()->back()->with('message', 'Kursas pridėtas. Jį galite matyti Kursų puslapyje.');
}
И js код, который помогает мне с зависимыми выпадающими списками:
<script type="text/javascript">
jQuery(document).ready(function ()
{
jQuery('select[name="city"]').on('change',function(){
var cityID = jQuery(this).val();
if(cityID)
{
jQuery.ajax({
url : 'findSteamCenter/' +cityID,
type : "GET",
dataType : "json",
success:function(data)
{
console.log(data);
jQuery('select[name="steam"]').empty();
jQuery('select[name="steam"]').append('<option value="">Steam centras</option>');
jQuery.each(data, function(key,value){
$('select[name="steam"]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
});
}
else
{
$('select[name="steam"]').empty();
$('select[name="room"]').empty();
jQuery('select[name="room"').append('<option value="">kambarys</option>');
}
});
jQuery('select[name="steam"]').on('change',function(){
var steamID = jQuery(this).val();
if(steamID)
{
jQuery.ajax({
url : 'findRoom/' +steamID,
type : "GET",
dataType : "json",
success:function(data)
{
console.log(data);
jQuery('select[name="room"]').empty();
jQuery('select[name="room"').append('<option value="">kambarys</option>');
jQuery.each(data, function(key,value){
$('select[name="room"]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
});
}
else
{
$('select[name="room"]').empty();
}
});
JQuery('select[name=""]')
});
</script>
Так что да, куда я должен поместить эти города-> id, steam-> id, room-> id. Спасибо за любой ответ. :)