簡體   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