Sync()方法更新数据透视表

时间:2016-09-17 09:31:49

标签: php laravel eloquent synchronization laravel-5.2

我尝试使用列approvals_document更新我的数据透视表isApprove,这是一个额外的列。我使用了这种方法,所以它不会在我的数据透视表中重复。

查看

@foreach ($pendingDocuments as $list)
    <form class = "form-inline" id="submitMe" method = "post" action = "{{ url('documents/pending') }}">

        <input type = "hidden" name = "id" value = "{{ $list->id }}">

        <div class = "radio">
            <label><input type = "radio" onclick = "showApprove()" name = "status" value = "1"> Approve</label>
        </div>


        <div class = "radio">
            <label><input type = "radio" onclick = "showReject()" name = "status" value = "2"> Reject</label>
        </div>

        <input type = "hidden" name = "_token" value = "{{ Session::token() }}">

    </form>
@endforeach

控制器

public function updateApprovalsDocument(Request $request, $id)
{
    $document = DB::table('approvals_document')
        ->select('approvals_document.id', 'approvals_document.isApprove')
        ->where('approvals_document.id', '=', $id)
        ->first();


    foreach($request->status as $status)
    {
        $document->sentToApprovers()->sync([$status => ['isApprove' => $request->status, 'updated_at' => new DateTime]], true);
    }


    return redirect()->back();
 }

$status这是我视图中单选按钮的名称。我是否需要在此处使用first()方法来使用单选按钮更新我的记录?

路由

Route::get('/documents/pending',
[
   'uses' => '\App\Http\Controllers\DocumentController@documentsSentForApproval',
   'as' => 'document.pending',
]);

Route::post('/documents/pending',
[
   'uses' => '\App\Http\Controllers\DocumentController@updateApprovalsDocument',
    'as' => 'document.pending',
]);

错误

Missing argument 2 for App\Http\Controllers\DocumentController::updateApprovalsDocument()

但我已在参数中定义$idRequest $request,并在use Illuminate\Http\Request;顶部导入任何帮助我该如何解决此问题?

更新

查看

<form class = "form-inline" id="submitMe" method = "post" action = "{{ route('documents/pending',['id' => [$list->id]]) }}">

            <div class = "radio">
                <label><input type = "radio" onclick = "showApprove()" name = "status" value = "1"> Approve</label>
            </div>


            <div class = "radio">
                <label><input type = "radio" onclick = "showReject()" name = "status" value = "2"> Reject</label>
            </div>

        <input type = "hidden" name = "_token" value = "{{ Session::token() }}">

    </form>

控制器

public function updateApprovalsDocument(Request $request)
{
    $id = $request->get('id'); //add this to get the id
    $document = DB::table('approvals_document')
        ->select('approvals_document.id', 'approvals_document.isApprove')
        ->where('approvals_document.id', '=', $id)
        ->first();

    $document->sentToApprovers()->sync([$request->status => ['isApprove' => $request->status, 'updated_at' => new DateTime]], true);
}

1 个答案:

答案 0 :(得分:1)

从方法的参数列表中删除$id

public function updateApprovalsDocument(Request $request)
{
    $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();
 }