Francisunoxx Francisunoxx - 2 months ago 10
PHP Question

Sync() method to update pivot table

I'm trying to update my pivot table

approvals_document
with a column
isApprove
which is a extra column. I used this method so it will not duplicated in my pivot table.

View

@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


Controller

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
here is the name of the Radio Button in my view. Do I need to used
first()
method here to update my record using radio button?

routes

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',
]);


Error

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


But I already define
$id
and
Request $request
in my parameter and imported at the top the
use Illuminate\Http\Request;
Any help how can I solve this problem?

UPDATE

View

<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>


Controller

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);
}

Answer

Remove $id from your method's argument list.

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();
 }