我正在尝试使用单选按钮更新我的数据透视表。我不需要去另一个网址的地方。但是在执行更新时它没有获得单选按钮的值。这是它的外观。
//SELECT
Route::get('/documents/pending/view/{id}',
[
'uses' => '\App\Http\Controllers\DocumentController@readDocumentsSentForApproval',
'as' => 'document.viewPending',
]);
//UPDATE
Route::post('/documents/pending/view/{id}',
[
'uses' => '\App\Http\Controllers\DocumentController@updateApprovalsDocument',
'as' => 'document.viewPending',
]);
控制器:
public function readDocumentsSentForApproval($id)
{
$viewPendingDocuments = DB::table('approvals_document')
->select('documents.title', 'documents.content', 'categories.category_type', 'documents.id')
->join('documents', 'documents.id', '=', 'approvals_document.document_id')
->join('categories', 'categories.id', '=', 'documents.category_id')
->where('documents.id', '=', $id)
->first();
$getApprovers = DB::table('approvals_document')
->select('users.first_name', 'users.middle_name', 'users.last_name', 'users.username', 'approvals_document.updated_at', 'approvals_document.isApprove', 'approvals_document.id', 'approvals_document.approver_id')
->join('documents', 'documents.id', '=', 'approvals_document.document_id')
->join('users', 'users.id', '=', 'approvals_document.approver_id')
->where('documents.id', '=', $id)
->get();
return view ('document.viewPending')
->with('viewPendingDocuments', $viewPendingDocuments)
->with('getApprovers', $getApprovers);
}
public function updateApprovalsDocument(Request $request)
{
//Getting the hidden input named = id.
$id = $request->get('id');
$document = DB::table('approvals_document')
->where('approvals_document.id', '=', $id)
->update(['isApprove' => $request->status, 'updated_at' => new DateTime]);
return redirect()->back();
}
查看
@foreach($getApprovers as $list)
<tr>
<td>
@if(Auth::id() == $list->approver_id)
<form class="form-inline" id="submitMe" method="post" action="{{ url('documents/pending/view' . $list->id) }}">
<input type="hidden" name="id" value="{{ $list->id }}">
<div class="form-group">
<label>
<input type="radio" onclick="showApprove()" name="status" value="1">Approve</label>
<label>
<input type="radio" onclick="showReject()" name="status" value="2">Reject</label>
</div>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
@endif
</td>
</tr>
@endforeach
我在动作上传递了当前的网址,因此它会知道id
是什么。这是更新的正确方法吗?谢谢你的帮助!
答案 0 :(得分:1)
它没有任何问题,但我认为Laravel推荐的方法是使用patch
路由,因为这是RESTful方式。
post
将用于创建实体,patch
用于更新实体。
您需要做的唯一更改是路径绑定(发布到补丁)和为方法添加表单欺骗
<input type="hidden" name="_method" value="PATCH">
https://laravel.com/docs/5.3/routing#form-method-spoofing
您也可以使用路径绑定作为id,而不是将其作为表单字段传递。如果您想对该路由进行验证(例如中间件,只允许该记录的所有者更新它),这将更有利。
答案 1 :(得分:0)
我已经使用表单方法欺骗
找到了解决方案public function readDocumentsSentForApproval($id)
{
$viewPendingDocuments = DB::table('approvals_document')
->select('documents.title', 'documents.content', 'categories.category_type', 'documents.id')
->join('documents', 'documents.id', '=', 'approvals_document.document_id')
->join('categories', 'categories.id', '=', 'documents.category_id')
->where('documents.id', '=', $id)
->first();
$getApprovers = DB::table('approvals_document')
->select('users.first_name', 'users.middle_name', 'users.last_name', 'users.username', 'approvals_document.updated_at', 'approvals_document.isApprove', 'approvals_document.approver_id',
'approvals_document.id as approvalDocumentId', 'documents.id as documentId')
->join('documents', 'documents.id', '=', 'approvals_document.document_id')
->join('users', 'users.id', '=', 'approvals_document.approver_id')
->where('documents.id', '=', $id)
->get();
return view ('document.viewPending')
->with('viewPendingDocuments', $viewPendingDocuments)
->with('getApprovers', $getApprovers);
}
查看:
<form class = "form-inline" role = "form" id = "submitMe" method = "POST" action = "/documents/pending/{{ $list->documentId}}/view">
{{ method_field('PATCH') }}
<input type = "hidden" name = "id" value = "{{ $list->approvalDocumentId }}">
<div class = "form-group">
<label><input type = "radio" onclick = "showApprove()" name = "status" value = "1"> Approve</label>
<label><input type = "radio" onclick = "showReject()" name = "status" value = "2"> Reject</label>
</div>
<input type = "hidden" name = "_token" value = "{{ Session::token() }}">
</form>