Laravel Store到DB

时间:2017-03-13 15:01:34

标签: php jquery laravel laravel-5 blade

根据第一个选择选项,我在表单中有两个相关的下拉列表。 当我试图存储到数据库时,我得到以下问题....

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'];

2 个答案:

答案 0 :(得分:2)

超级简单,您的数据库中有一个名为service_id的字段,必须填写并具有值。

在你的php中,你希望表单字段名为serviceID,但在你的html中你称之为service

<select name="service" class="form-control" style="width:350px">

不同的名称,因此将其中一个更改为另一个。

答案 1 :(得分:0)

您的选择表单字段名称&#39; service&#39;但你尝试使用$ request-&gt; get(&#39; serviceID&#39;)来获取价值,这就是你得到错误的原因。 替换您的商店方法。

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');