[英]Update status in database using checkbox - Laravel
The project can have different statuses. 该项目可以具有不同的状态。 With the checkbox, I am trying to switch the status of the project from requested (1) to accepted (2).
选中该复选框,我试图将项目的状态从已请求(1)切换为已接受(2)。 If the checkbox is unchecked the status is 1, checked it's 2.
如果未选中此复选框,则状态为1,选中状态为2。
When I check the checkbox I got a 419 but this is normally related to the token but I added a @csfr field. 当我选中复选框时,我得到了419,但这通常与令牌有关,但是我添加了@csfr字段。 Why is the status not changed in the database?
为什么状态在数据库中未更改? Thanks for any help.
谢谢你的帮助。
@foreach ($projects as $project)
<tbody>
<tr>
<form action="/projects/plan" method="post" id="statusForm">
@csrf
<input name="id" type="hidden" value="{{$project->id}}">
<td>
<input type="hidden" value="{{$project->status}}" name="status">
<input {{isset($project['status']) && $project['status'] == '2' ? 'checked' : ''}}
value="{{$project->status}}" type="checkbox" name="status"
onchange="document.getElementById('statusForm').submit()"
>
</td>
</form>
<td>{{$project->applicant_name}}</td>
<td>{{$project->project_name}}</td>
<td><a href="/events/{{$projects->id}}/edit" class="btn btn-secondary btn-sm" role="button">Project Details</a></td>
</tr>
</tbody>
@endforeach
const STATUS_requested = 1;
const STATUS_ACCEPTED = 2;
public function updateStatus( $status )
{
$this->update([
'status' => $status
]);
$this->refresh();
return $this;
}
public function projectAccept() {
return $this->updateStatus( self::STATUS_ACCEPTED );
}
public function plan(Request $request)
{
dd('hello');
Event::find($request->id)->projectAccept();
return Project::STATUS_ACCEPTED;
}
// Update status project
Route::post('/projects/plan', 'ProjectsController@plan');
First, you cant select by ID document.getElementById('statusForm').submit()
when you have multiple DOMS with the same ID. 首先,如果您有多个具有相同ID的DOMS,则无法按ID
document.getElementById('statusForm').submit()
进行选择。
change your loop to something like this 改变你的循环像这样
@foreach ($projects as $project)
<tbody>
<tr>
<td>
<form action="/projects/plan" method="post" id="statusForm{{$project->id}}">
@csrf
<input name="id" type="hidden" value="{{$project->id}}">
<input {{isset($project['status']) && $project['status'] == '2' ? 'checked' : ''}}
value="2" type="checkbox" name="status"
onchange="document.getElementById('statusForm{{$project->id}}').submit()"
>
</form>
</td>
<td>{{$project->applicant_name}}</td>
<td>{{$project->project_name}}</td>
<td><a href="/events/{{$projects->id}}/edit" class="btn btn-secondary btn-sm" role="button">Project Details</a></td>
</tr>
</tbody>
@endforeach
Now, a checkbox will only be sent in a form when it is checked, so no need for a variable value for that input 现在,复选框仅在选中时才以表格形式发送,因此该输入不需要变量值
<input {{isset($project['status']) && $project['status'] == '2' ? 'checked' : ''}}
value="2" type="checkbox" name="status"
onchange="document.getElementById('statusForm{{$project->id}}').submit()"
>
Finally, when you recover the input status
, set a default value for the unchecked one (you can remove the hidden input with this one). 最后,当您恢复输入
status
,请为未选中的输入设置默认值(您可以使用此输入删除隐藏的输入)。 Or as you did, set a hidden input with the original value to be sent every time. 或者像您所做的那样,设置一个隐藏的输入,将其与每次发送的原始值一起发送。 Both solution are perfect.
两种解决方案都是完美的。
public function plan(Request $request)
{
$status = $request->input('status', Project::STATUS_requested);
Event::find($request->id)->projectAccept();
return Project::STATUS_ACCEPTED;
}
That way if it is checked it will be 2 (in the request) and if not, it will be 1 from the default value. 这样,如果选中它,它将是2(在请求中),否则,默认值将是1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.