我的项目出了什么问题。
我想使用选择选项进行保存,但该字段不保存数据选择选项。它只是保存输入文本。
该项目采用不同的表格,表格仅获得project_name
和project_id
。 project_id
将保存在表spent_times
中。
并且select选项将task_category
保存在表spent_times
这是我的模特
protected $table = 'spent_times';
protected $fillable = [
'task_category',
'story/meeting_name',
'assign',
'estimated_time',
'user_story',
'spent_time',
'percentage',
'lateness',
'index',
'project_id'
];
public function users() {
return $this->hasMany(User::class);
}
public function project() {
return $this->belongsTo(Project::class);
}
我的create.blade.php
<form action="{{route('store')}}" method="POST">
@csrf
<div class="box-body">
<div class="form-group">
<label for="">Project *</label>
<select class="form-control select2" style="width: 100%;">
<option>Select One</option>
@foreach($projects as $id => $project)
<option value="{{$id}}">{{$project}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="">Story # Meeting Name *</label>
<input type="text" class="form-control" name="user_story">
</div>
<div class="form-group">
<label for="">Category *</label>
<select class="form-control select2" style="width: 100%;">
<option>Select One</option>
@foreach($task_categories as $category)
<option value="{{$category}}">{{$category}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="">Estimated *</label>
<input type="text" class="form-control" name="estimated_time">
</div>
</div>
<div class="box-footer">
<a href="{{route('index')}}">
<button type="submit" class="btn btn-primary col-md-12" style="border-radius : 0px;">SAVE</button>
</a>
</div>
</form>
我的控制器
public function create()
{
$spentimes = new SpentTime;
$project = new Project;
$projects = Project::select('project_name', 'id')->get();
return view('Ongoings.index', compact ('projects', 'task_categories', 'spentimes', 'project'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
dd($request->all());
$spentime = SpentTime::create([
'project_name' => request('project_name'),
'user_stroy' => request ('user_story'),
'task_category' => request('task_category'),
'estimated_time' => request('estimated_time')
]);
$spentime->save();
return redirect()->route('index');
}
像这样的错误 error in web browser
请帮助我
答案 0 :(得分:0)
You are expecting those fields from request in controller.
project_id
meeting_name
task_category
estimated_time
But You are just sending estimated_time
,
other 3 fields are not present in you create.blade.php
In create.blade.php, category select input don't have any name.
that's why task_category
is getting null from request('task_category')
. But task_category
field is not nullable in your database table.
Send form input properly as you are expecting in your controller. You can use dd($request->all())
to check.
答案 1 :(得分:0)
我相信这是您的task_category
选择框。
因此此选择框没有 name属性。那是错误。
可能的解决方案。
<div class="form-group">
<label for="">Category *</label>
<select name="task_category" class="form-control select2" style="width: 100%;">
<option>Select One</option>
@foreach($task_categories as $category)
<option value="{{$category}}">{{$category}}</option>
@endforeach
</select>
</div>