2017-03-13 32 views
0

私のフォームには、最初の選択オプションに基づいて2つの依存ドロップダウンがあります。 私は次のような問題を取得するDBに保存しようとしています....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は、依存ドロップダウンのためです:モデルで

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

:ここ

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

は私のコントローラ店方式である

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

であなたの店の方法の第四行を置き換えるあなたの形式でいるServiceIDを渡していません。 – aynber

答えて

2

スーパーシンプル、フィールドがありますあなたのデータベースにservice_idと書かれていなければなりません。

PHPでは、serviceIDというフォームフィールドが必要ですが、htmlではserviceと呼びます。

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

異なる名前なので、どちらか一方を他方に変更してください。

+0

私は間違った方法でselect要素のオプションを埋めると信じています。 生成されたHTMLをチェックして、ここに書き込みます。 –

0

あなたの選択フォームのフィールド名は「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('/'); 
} 

それとも

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