Ich habe in meiner Form zwei abhängige Dropdowns basierend auf der ersten Auswahlmöglichkeit. Wenn ich an die DB zu speichern versucht bin ich folgendes Problem bekommen ....Laravel Store zu DB
Integrity constraint violation: 1048 Column 'service_id' cannot be null
Kann mir jemand helfen herauszufinden, wo mein Problem ist und wie sie zu beheben, ich denke, es zu meinem Speicher-Methode verwandt ist, weil die abhängigen dropwdowns
Hier ist, wie ich zu der Ansicht passieren:
$services = \DB::table('services')->lists("name", "id");
return view ('reservations', compact('services'));
Hier ist meine Form in Sicht ist:
{!! 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="{{ $key }}">{{ $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() !!}
Hier
ist die JS für abhängige Dropdown-Listen:
$(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();
}
});
});
Hier ist meine Controller Methode speichern:
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('/');
}
im Modell:
protected $fillable = ['service_price', 'service_time','booking_date', 'booking_time'];
Sie sind nicht die serviceid in Ihrem Formular übergeben. – aynber