简体   繁体   中英

Laravel Store to DB

I have in my form two dependant dropdowns based on the first select option. When i am attempting to store to the DB i get the following problem....

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

Can someone help me identify where my problem is and how to fix, i think it is related to my store method because the dependant dropwdowns

Here is how i pass to the view:

 $services = \DB::table('services')->lists("name", "id");
    return view ('reservations', compact('services'));

Here is my form in view:

  {!! 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() !!}

Here is the JS for dependant dropdowns:

$(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();
        }
    });
});

Here is my controller store method:

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('/');
}

in the model:

  protected $fillable = ['service_price', 'service_time','booking_date', 'booking_time'];

Super simple, there is a field in your database called service_id that must be filled and have a value.

In your php, you expect form field called serviceID , but in your html you call it service .

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

Different names, so change one of them to the other.

Your selection form field name 'service' but you try to get value using $request->get('serviceID') that's why your got error. Replace your store method.

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('/');
}

Or replace 4th line of your store method with

  $bookings->service_id = $request->get('service');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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