繁体   English   中英

Laravel存储到数据库

[英]Laravel Store to DB

在表单中,我有两个基于第一个选择选项的依赖下拉列表。 当我尝试存储到数据库时,出现以下问题...。

Integrity constraint violation: 1048 Column 'service_id' cannot be null 

有人可以帮我确定问题出在哪里以及如何解决,我认为这与我的存储方法有关,因为依赖的dropwdowns

这是我传递给视图的方式:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM