Laravel 存储到数据库
Laravel Store to DB
我的表单中有两个基于第一个 select 选项的相关下拉列表。
当我尝试存储到数据库时出现以下问题....
Integrity constraint violation: 1048 Column 'service_id' cannot be null
谁能帮我确定我的问题出在哪里以及如何解决,我认为这与我的存储方法有关,因为相关的下拉列表
这是我传递给视图的方式:
$services = \DB::table('services')->lists("name", "id");
return view ('reservations', compact('services'));
这是我的表格:
{!! 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() !!}
这是依赖下拉列表的 JS:
$(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();
}
});
});
这是我的控制器存储方法:
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('/');
}
模型中:
protected $fillable = ['service_price', 'service_time','booking_date', 'booking_time'];
超级简单,您的数据库中有一个名为 service_id
的字段必须填写并具有值。
在您的 php 中,您希望表单字段名为 serviceID
,但在您的 html 中,您将其称为 service
.
<select name="service" class="form-control" style="width:350px">
不同的名称,所以将其中一个更改为另一个。
您的选择表单字段名称 'service' 但您尝试使用 $request->get('serviceID') 获取值,这就是您出错的原因。
替换您的存储方法。
public function store(Request $request,int $service)
{
//
$data = \Input::only("booking_date", "booking_time");
$bookings = new Booking($data);
$bookings->user_id = auth()->user()->id;
$bookings->service_id = $request->get('service');
$bookings->service_price = $request->get('price');
$bookings->booking_date = $request->get('booking_date');
$bookings->booking_time = $request->get('booking_time');
$bookings->save();
return redirect('/');
}
或者将存储方法的第 4 行替换为
$bookings->service_id = $request->get('service');
我的表单中有两个基于第一个 select 选项的相关下拉列表。 当我尝试存储到数据库时出现以下问题....
Integrity constraint violation: 1048 Column 'service_id' cannot be null
谁能帮我确定我的问题出在哪里以及如何解决,我认为这与我的存储方法有关,因为相关的下拉列表
这是我传递给视图的方式:
$services = \DB::table('services')->lists("name", "id");
return view ('reservations', compact('services'));
这是我的表格:
{!! 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() !!}
这是依赖下拉列表的 JS:
$(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();
}
});
});
这是我的控制器存储方法:
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('/');
}
模型中:
protected $fillable = ['service_price', 'service_time','booking_date', 'booking_time'];
超级简单,您的数据库中有一个名为 service_id
的字段必须填写并具有值。
在您的 php 中,您希望表单字段名为 serviceID
,但在您的 html 中,您将其称为 service
.
<select name="service" class="form-control" style="width:350px">
不同的名称,所以将其中一个更改为另一个。
您的选择表单字段名称 'service' 但您尝试使用 $request->get('serviceID') 获取值,这就是您出错的原因。 替换您的存储方法。
public function store(Request $request,int $service)
{
//
$data = \Input::only("booking_date", "booking_time");
$bookings = new Booking($data);
$bookings->user_id = auth()->user()->id;
$bookings->service_id = $request->get('service');
$bookings->service_price = $request->get('price');
$bookings->booking_date = $request->get('booking_date');
$bookings->booking_time = $request->get('booking_time');
$bookings->save();
return redirect('/');
}
或者将存储方法的第 4 行替换为
$bookings->service_id = $request->get('service');