I have in my form two dependant dropdowns based on the first select option. When i am attempting to store to the DB i get the following problem....
Integrity constraint violation: 1048 Column 'service_id' cannot be null
Can someone help me identify where my problem is and how to fix, i think it is related to my store method because the dependant dropwdowns
Here is how i pass to the view:
$services = \DB::table('services')->lists("name", "id");
return view ('reservations', compact('services'));
Here is my form in view:
{!! Form::open(array("url"=>"bookings", "class"=>"form-horizontal")) !!}
<select name="service" class="form-control" style="width:350px">
<option value="">--- Select Service ---</option>
@foreach ($services as $key => $value)
<option value=""></option>
@endforeach
</select>
<br />
<label for="price">This cost for this service is:</label><br />
<select name="price">
<option id="price"></option>
</select><br />
<label for="time">This duration for this service is:</label><br />
<select name="time">
<option id="time"></option>
</select>
<br />
<br />
{!! Form::label("booking_date", "Enter Date", array("class"=>"col-md-2")) !!}
{!! Form::text("booking_date","",array("placeholder"=>"Enter Date", "class="=>"form-control")) !!}
<br />
<br />
{!! Form::label("booking_time", "Enter Time", array("class"=>"col-md-2")) !!}
{!! Form::text("booking_time","",array("placeholder"=>"Enter Time", "class="=>"form-control")) !!}
<br />
<br />
{!! Form::submit('Book', array("class"=>"btn btn-primary","id"=>"btn")) !!}
{!! Form::close() !!}
Here is the JS for dependant dropdowns:
$(document).ready(function() {
$('select[name="service"]').on('change', function() {
var serviceID = $(this).val();
if(serviceID) {
$.ajax({
url: '/myform/ajax/'+serviceID,
type: "GET",
dataType: "json",
success:function(data) {
$('option[id="price"]').empty();
$.each(data, function(key, value) {
$('option[id="price"]').append('<p value="'+ key +'">£'+ value +'</p>');
});
}
});
}else{
$('option[id="price"]').empty();
}
});
});
$(document).ready(function() {
$('select[name="service"]').on('change', function() {
var serviceID = $(this).val();
if(serviceID) {
$.ajax({
url: '/serviceTime/ajax/'+serviceID,
type: "GET",
dataType: "json",
success:function(data) {
$('option[id="time"]').empty();
$.each(data, function(key, value) {
$('option[id="time"]').append('<p value="'+ key +'">'+ value +' minutes</p>');
});
}
});
}else{
$('option[id="time"]').empty();
}
});
});
Here is my controller store method:
public function store(Request $request)
{
//
$data = \Input::only("booking_date", "booking_time");
$bookings = new Booking($data);
$bookings->user_id = auth()->user()->id;
$bookings->service_id = $request->get('serviceID');
$bookings->service_price = $request->get('price');
$bookings->booking_date = $request->get('booking_date');
$bookings->booking_time = $request->get('booking_time');
$bookings->save();
return redirect('/');
}
via Jay240692