[英]updateExistingPivot won't work
I'm trying to update my pivot table approve_document where it has a extra column isApprove
using ->withPivot
method. 我正在尝试使用->withPivot
方法更新我的数据透视表approve_document ,其中它具有额外的列isApprove
。
Model: 模型:
Document 文献
class Document extends Model
{
public function sentToApprovers()
{
return $this->belongsToMany('App\Models\Approve', 'approve_document')->withPivot('isApprove');
}
}
Approve 批准
class Approve extends Model
{
public function createdpendingDocuments()
{
return $this->belongsToMany('App\Models\Document', 'approve_document')->withPivot('isApprove');
}
}
This is where I get all my records in my approve_document . 这是我将所有记录保存在approve_document中的位置 。
Controller: 控制器:
public function documentsSentForApproval()
{
$pendingDocumentLists = DB::table('approve_document')
->select('documents.title', 'documents.content', 'categories.category_type', 'users.username', 'approve_document.dateReceived', 'documents.id', 'approves.approver_id')
->join('documents', 'documents.id', '=', 'approve_document.document_id')
->join('categories', 'categories.id', '=', 'documents.category_id')
->join('approves', 'approves.id', '=', 'approve_document.approve_id')
->join('users', 'users.id', '=', 'approves.approver_id')
->where('approver_id', '=', Auth::id())
->orWhere('requestedBy', '=', Auth::id())
->get();
return view ('document.pending')
->with('pendingDocumentLists', $pendingDocumentLists);
}
View: 视图:
@foreach ($pendingDocumentLists as $list)
<tr class = "info">
<td>{{ $list->title }}</td>
<td>{{ strip_tags(substr($list->content, 0, 50)) }} {{ strlen($list->content) > 50 ? "..." : '' }}</td>
<td>{{ $list->category_type }}</td>
<td>{{ $list->username }}</td>
<td>{{ date('M, j, Y', strtotime($list->dateReceived)) }}</td>
<td>
@if (Auth::id() == $list->approver_id)
<a href = "{{ route ('document.pending', $list->id) }}">
<button type = "submit" class = "btn btn-success glyphicon glyphicon-thumbs-up"> Approve</button>
</a>
@endif
</td>
<td></td>
</tr>
@endforeach
You can see here I have a approve button where I need to set isApprove
to true when the button is clicked. 您可以在此处看到一个批准按钮,单击该按钮时需要将isApprove
设置为true。 You can see that I get the current id of the document when the button was clicked. 您可以看到单击按钮后,我得到了文档的当前ID。
This part of the Controller where I'm having a hard time updating my pivot table. 我很难在控制器的这一部分更新数据透视表。 It gives me a error MethodNotAllowedHttpException
. 它给我一个错误MethodNotAllowedHttpException
。 Any tips or help would greatly appreciated! 任何提示或帮助将不胜感激!
public function updateIsApprove($id)
{
$document = new Document();
foreach ($document as $update)
{
$approve = new Approve();
$document->sentToApprovers()->updateExistingPivot([$approve->id => ['isApprove' => '1']],false);
}
return redirect()->route('document.pending');
}
routes: 路线:
Route::post('/documents/pending/approve/{id}',
[
'uses' => '\App\Http\Controllers\DocumentController@updateIsApprove',
'as' => 'document.pending',
]);
public function updateExistingPivot($id, array $attributes, $touch = true)
第一个参数应该是相关事物的ID。
public function updateIsApprove($documentId, $approveId)
{
$document = Document::find($documentId);
if (!$document) {
// Handle error that document not exists.
}
$approve = $document->sentToApprovers()->find($approveId);
if (!$approve) {
// Handle that approve not exists or is not related with document.
}
$document->sentToApproves()->updateExistingPivot($approve->id, ['isApprove' => 1]);
return redirect()->route('document.pending');
}
MethodNotAllowedHttpException
is not for your controller but is for your Route
. MethodNotAllowedHttpException
不适用于您的控制器,但适用于Route
。 As you can see, in your Routes file, you have Route for handling POST
request, but in your view you are making a GET
request by accessing the URL directly. 如您所见,在Routes文件中,您具有用于处理POST
请求的Route,但是在您看来,您是通过直接访问URL来发出GET
请求的。
So, just change the POST
route to GET
in your Routes file. 因此,只需在Routes文件中将POST
路由更改为GET
。
Route::get('/documents/pending/approve/{id}',
[
'uses' => '\App\Http\Controllers\DocumentController@updateIsApprove',
'as' => 'document.pending',
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.