2016-08-19 4 views
0

Ich versuche, meine Pivot-Tabelle zu aktualisieren approve_document, wo es eine zusätzliche Spalte isApprove mit ->withPivot Methode hat.updateExistingPivot wird nicht funktionieren

Modell:

Dokument

class Document extends Model 
{ 
    public function sentToApprovers() 
    { 
    return $this->belongsToMany('App\Models\Approve', 'approve_document')->withPivot('isApprove'); 
    } 
} 

Genehmigen

class Approve extends Model 
{ 
    public function createdpendingDocuments() 
    { 
    return $this->belongsToMany('App\Models\Document', 'approve_document')->withPivot('isApprove'); 
    } 
} 

Dies ist, wo ich in meinem approve_document alle meine Aufzeichnungen erhalten.

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

Ausblick:

@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 

Sie können hier sehen, ich habe eine Schaltfläche genehmigen, wenn ich brauche isApprove auf true gesetzt, wenn die Schaltfläche geklickt wird. Sie sehen, dass ich beim Klicken auf die Schaltfläche die aktuelle ID des Dokuments erhalte.

Dieser Teil des Controllers, in dem es mir schwer fällt, meine Pivot-Tabelle zu aktualisieren. Es gibt mir einen Fehler MethodNotAllowedHttpException. Irgendwelche Tipps oder Hilfe würden sehr geschätzt!

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

Routen:

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

Antwort

0
public function updateExistingPivot($id, array $attributes, $touch = true) 

Erste parametr id verwandter Sache sein sollte.

+0

Ist dies optional? Ich habe bei Laravel Dokumentation nichts gefunden. – Francisunoxx

+0

Es ist erforderlich. Sehen Sie sich mein Beispiel an: $ comany-> members() -> updateExistingPivot ($ member-> id, ['is_admin' => true]); – Vuer

+0

Ich habe nichts falsches mit Ihrem Code gesehen, aber ich habe versucht, folgen, aber nichts passiert, es wird nicht in meine Pivot-Tabelle aktualisiert. – Francisunoxx

0
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'); 
} 
+0

Ich habe einen Fehler bekommen Ich denke, ich habe etwas verpasst?Es heißt 'Fehlendes Argument 2 für App \ Http \ Controllers \ DocumentController :: updateIsApprove()' Welche Argumente fehlen mir hier? – Francisunoxx

+0

Sie haben kein Argument 2: D Sie müssen es in der Route angeben. – Vuer

+0

Meine Route sieht so aus. 'Route :: get ('/ documents/pending/approve/{id}', [ \t 'verwendet' => \ App \ Http \ Controller \ DocumentController @ updateIsApprove ', \t' als '=>' Dokument .pending ', ]); 'Hab ich etwas verpasst? – Francisunoxx

0

MethodNotAllowedHttpException ist für Ihren Controller nicht aber für Ihre Route. Wie Sie sehen können, haben Sie in Ihrer Routes-Datei Route zur Bearbeitung der Anfrage POST, aber in Ihrer Sicht machen Sie eine GET Anfrage, indem Sie direkt auf die URL zugreifen.
Ändern Sie einfach die POST Route zu GET in Ihrer Routes-Datei.

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