updateExistingPivot将无法正常工作

时间:2016-08-19 18:25:22

标签: php laravel eloquent laravel-5.2 pivot-table

我正在尝试使用val newArray: Array[Node] = words.map(w => new Node(w, 9, "d", 0)) val newVector: Vector[Node] = newArray.toVector val optVector: Option[Vector[Node]] = Option(newVector) // Or val optVector = Option(words.map(w => new Node(w, 9, "d", 0)).toVector) 方法更新我的数据透视表 approve_document ,其中有一个额外的列isApprove

型号:

文档

->withPivot

批准

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

这是我在 approve_document 中获取所有记录的地方。

控制器:

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

查看:

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

你可以在这里看到我有一个批准按钮,我需要在单击按钮时将@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 设置为true。您可以看到我在单击按钮时获得文档的当前ID。

控制器的这一部分,我很难更新我的数据透视表。它给了我一个错误isApprove。任何提示或帮助将非常感谢!

MethodNotAllowedHttpException

路线:

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

3 个答案:

答案 0 :(得分:0)

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

第一个参数应该是相关事物的id。

答案 1 :(得分: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');
}

答案 2 :(得分:0)

MethodNotAllowedHttpException不适用于您的控制器,而是适用于您的Route。如您所见,在您的路由文件中,您有处理POST请求的路由,但在您的视图中,您通过直接访问该网址发出GET请求。
因此,只需将路由文件中的POST路由更改为GET

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