2017-03-05 176 views
0

這裏新增了laravel框架。我在這裏調用我的資源控制器中的刪除功能時遇到了相當大的問題。似乎不會刪除選定的ID。感謝您的幫助提前。刪除功能:Laravel 5

資源/視圖/ bufashaccts/allAccounts.blade.php

@extends('adminlte::page') 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title></title> 
</head> 
<body> 
<h1>view accounts!</h1> 
@foreach($bfaccounts as $userAccount) 
    <p>{{ $userAccount->acct_firstname }}</p><br> 
    <p>{{ $userAccount->acct_middlename }}</p><br> 
    <p>{{ $userAccount->acct_lastname }}</p> 
    @if ($userAccount->id) 
     <form action="/Accounts" method="POST"> 
      {{ csrf_field() }} 
      {{ method_field('DELETE') }} 
      <a href="/Accounts"> 
       <button type="button">delete</button> 
      </a> 
     </form> 
    @endif 
    <a href="/Accounts/{{ $userAccount->id }}/edit"> 
     <button type="button">edit</button> 
    </a> 
@endforeach 
</body> 
</html> 

應用程序/ HTTP /控制器/ AccountsController.php

<?php 

namespace App\Http\Controllers; 

use App\bufashaccounts; 
use Illuminate\Http\Request; 

class AccountsController extends Controller 
{ 

    public function index() 
    { 
     $bfaccounts = bufashaccounts::all(); 

     return view('bufashaccts.allAccounts', compact('bfaccounts')); 
    } 

    public function create() 
    { 
     return view('bufashaccts.addAccounts'); 
    } 

    public function store(Request $request) 
    { 
     bufashaccounts::create($request->all()); 

     return "success!"; 
    } 

    public function show($id) 
    { 
     $bfshowAccounts = bufashaccounts::findOrFail($id); 

     return view('bufashaccts.viewAccounts', compact('bfshowAccounts')); 
     //return $bfshowAccounts; 
    } 

    public function edit($id) 
    { 
     $bfeditAccounts = bufashaccounts::findOrFail($id); 

     return view('bufashaccts.editAccounts', compact('bfeditAccounts')); 
    } 

    public function update(Request $request, $id) 
    { 
     $bfeditAccounts = bufashaccounts::find($id); 
     $bfeditAccounts->update($request->all()); 

     return redirect('Accounts'); 
    } 

    public function destroy($id) 
    { 
     //$bfdeleteAccounts = bufashaccounts::findOrFail($id); 
     //$bfdeleteAccounts->delete(); 
     //return 'delete'; 
     $bfaccounts = bufashaccounts::findOrFail($id); 
     $bfeditAccounts->delete(); 

     //return view('bufashaccts.allAccounts', compact('bfaccounts')); 
     return redirect('/Accounts'); 
    } 
} 
+1

爲什麼你在鏈接中有一個按鈕? –

+0

你是如何爲它設置路線的? – Sina

+0

我還沒有爲它設置路由,因爲它是一個資源路由 – JING

回答

0

你需要改變你的形式是這樣的:

<form action="{{ url("/Accounts/$userAccount->id") }}" method="POST"> 
    {{ csrf_field() }} 
    {{ method_field('DELETE') }} 
    <button type="submit">delete</button> 
</form> 

希望這會有所幫助!

+0

沒有看到那個按鈕類型。但是現在它返回一個'MethodNotFoundHttpException'。將不會迴應刪除或發佈方法 – JING

+0

你可以編輯你的問題並顯示你的控制器的'路由'? 'URL'語法幫助的 –

+0

。必須進一步審查。但是,謝謝你的迴應 – JING

0
  1. 在你的路由文件添加此

    Route::delete('account/delete/{id}', ['as' => 'account.delete', 'uses' => '[email protected]']) 
    
  2. 修改您的刀片文件

    <form action="{{ route('account.delete') }}" method="POST"> 
    {{ csrf_field() }} 
    {{ method_field('DELETE') }} 
    <button type="submit" class="btn btn-danger">Delete</button> 
    

嘗試這種方式。希望它能起作用。