我有数据库表“事件”和“类别”。我想使用表单中的选择下拉菜单为事件分配类别。我还希望能够对其进行编辑和更新。我目前给了我以下错误的代码 -
SQLSTATE [23000]:违反完整性约束:1048 列 'category_id' 不能为空
addEvent.blade.php
<div class="form-group">
<div class="form-group">
<div class="form-group">
{!! Form::Label('category_id', 'Category:') !!}
<select> class="form-control" name="category_id">
@foreach($categories as $category)
<option value='{{ $category->id}}'> {{ $category->category}}</option>
@endforeach
</select>
</div>
</div>
</div>
事件控制器
public function addEvent(Request $request)
{
$this->validate($request, [
'event_name' => 'required',
'start_date' => 'required',
'end_date' => 'required',
'time' => 'required',
'trip_id' => 'required',
]);
$start_date = Carbon::parse($request['start_date'])->format('Y-m-d');
$end_date = Carbon::parse($request['end_date'])->format('Y-m-d');
$tripCheck = Trip::where('id', $request['trip_id'])
->whereDate('startdate', '<=', $start_date)
->whereDate('enddate', '>=', $start_date)
->whereDate('startdate', '<=', $end_date)
->whereDate('enddate', '>=', $end_date)
->first();
if ($tripCheck) {
$events = new Events;
$trips = Trip::all();
$categories = Categories::pluck('category','id');
$events->category_id = $request['category_id'];
$events->colour = $request['colour'];
$events->event_name = $request['event_name'];
$events->start_date = $request['start_date'];
$events->end_date = $request['end_date'];
$events->time = $request['time'];
$events->address = $request['address'];
$events->notes = $request['notes'];
$events->trip_id = $request['trip_id'];
$events->save();
return redirect('trips')->with('success', 'The new event has been added to your trip')->with('trips', $trips)->withCategories($categories);
} else
{
return redirect('trips')->withErrors(['The dates you added are not within Trip start and end date.']);
}
犯罪嫌疑人X