2016-10-10 2 views
0

Ich versuche, meine Pivot-Tabelle mit Radio-Button zu aktualisieren. Wo ich nicht zu einer anderen URL gehen muss. Die Werte des Optionsfelds werden bei der Aktualisierung jedoch nicht angezeigt. Hier, wie es aussieht.Aktualisierung auf der aktuellen ID

//SELECT 
Route::get('/documents/pending/view/{id}', 
[ 
    'uses' => '\App\Http\Controllers\[email protected]', 
    'as' => 'document.viewPending', 
]); 
//UPDATE 
Route::post('/documents/pending/view/{id}', 
[ 
    'uses' => '\App\Http\Controllers\[email protected]', 
    'as' => 'document.viewPending', 
]); 

Controller:

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

} 

Ansicht

@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 

habe ich die aktuelle URL auf die Aktion, damit es weiß, was id es ist. Ist das die richtige Art der Aktualisierung? Danke für Ihre Hilfe!

Antwort

1

Es ist nichts falsch daran, aber ich denke, der empfohlene Weg in Laravel wäre die Verwendung der patch Route, da dies die RESTful Art ist, es zu tun.

post würde zum Erstellen der Entität verwendet werden, patch wird für die Aktualisierung verwendet.

Die einzigen Änderungen, die Sie machen müssten, ist der Weg (Post zu patchen) Bindung und das Hinzufügen der Form Spoofing für die Methode <input type="hidden" name="_method" value="PATCH">

https://laravel.com/docs/5.3/routing#form-method-spoofing

Sie auch die Route verbindlich für die verwenden könnte ID, anstatt es als Formularfeld zu übergeben. Dies wäre vorteilhafter, wenn Sie eine Validierung auf dieser Route durchführen möchten (z. B. eine Middleware, die nur dem Besitzer dieses Datensatzes erlaubt, diese zu aktualisieren).

+0

finde ich schon eine Lösung mit einer Verwendung von Form Modell Bindung: D – Francisunoxx

0

ich bereits eine Lösung für diesesFormularmethode Spoofing mit finden

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

Ausblick:

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