title: Laravel - Form select categories from database
body: <p>I have database tables 'Events' and 'Categories'. 
  I want to assign a category to an event using a select dropdown in the form. I also want to be able to edit it and update it. The code I have currently given me the following error - </p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1048 Column
    'category_id' cannot be null</p>
  </blockquote>
  
  <p><strong>addEvent.blade.php</strong></p>
  
  <pre><code>  &lt;div class="form-group"&gt;
      &lt;div class="form-group"&gt;
        &lt;div class="form-group"&gt;
          {!! Form::Label('category_id', 'Category:') !!}
          &lt;select&gt; class="form-control" name="category_id"&gt;
            @foreach($categories as $category)
              &lt;option value='{{ $category-&gt;id}}'&gt; {{ $category-&gt;category}}&lt;/option&gt;
            @endforeach
           &lt;/select&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  </code></pre>
  
  <p><strong>eventController</strong></p>
  
  <pre><code>public function addEvent(Request $request)
  {
    $this-&gt;validate($request, [
      'event_name'  =&gt; 'required',
      'start_date'  =&gt; 'required',
      'end_date'    =&gt; 'required',
      'time'        =&gt; 'required',
      'trip_id'     =&gt; 'required',
  
    ]);
  
      $start_date = Carbon::parse($request['start_date'])-&gt;format('Y-m-d');
      $end_date = Carbon::parse($request['end_date'])-&gt;format('Y-m-d');
  
      $tripCheck = Trip::where('id', $request['trip_id'])
      -&gt;whereDate('startdate', '&lt;=', $start_date)
      -&gt;whereDate('enddate', '&gt;=', $start_date)
  
      -&gt;whereDate('startdate', '&lt;=', $end_date)
      -&gt;whereDate('enddate', '&gt;=', $end_date)
      -&gt;first();
  
      if ($tripCheck) {
  
        $events = new Events;
        $trips = Trip::all();
        $categories = Categories::pluck('category','id');
        $events-&gt;category_id = $request['category_id'];
        $events-&gt;colour =  $request['colour'];
        $events-&gt;event_name = $request['event_name'];
        $events-&gt;start_date = $request['start_date'];
        $events-&gt;end_date = $request['end_date'];
        $events-&gt;time = $request['time'];
        $events-&gt;address = $request['address'];
        $events-&gt;notes = $request['notes'];
        $events-&gt;trip_id = $request['trip_id'];
        $events-&gt;save();
  
  return redirect('trips')-&gt;with('success', 'The new event has been added to your trip')-&gt;with('trips', $trips)-&gt;withCategories($categories);
  } else
    {
    return redirect('trips')-&gt;withErrors(['The dates you added are not within Trip start and end date.']);
     }
  
  
  public function display($id)
  {
    $categories = Categories::all();
  
    return view('/addEvent')-&gt;with('trip_id', $id)-&gt;withCategories($categories);
  }
  </code></pre>
  
  <p><strong>editEvent</strong></p>
  
  <pre><code>&lt;div class="form-group"&gt; Category:
        &lt;input type="select" name="category" class="form-control" value="{{$events-&gt;category}}" placeholder="Category" /&gt;
      &lt;/div&gt;
  </code></pre>