簡體   English   中英

如何使用資源豐富的路由刪除Laravel 4.2中的單個記錄?

[英]How to delete individual records in Laravel 4.2 using resourceful routing?

我正在Laravel 4.2中構建一個“食譜”應用程序,並為食譜和類別設置了資源豐富的路由。 在category / {$ id} / edit頁面(edit.blade.php)上,我有一個綁定模型的表單來編輯類別,在其下,我還有一個附加的表單發布到CategoryController@destroy方法中,以進行刪除,但是每次嘗試時,它都會拋出MethodNotAllowedHttpException (這是一個受保護的方法)。 我在我的destroy($ id)函數上嘗試了“刪除”和“銷毀”,但是每個都拋出相同的錯誤。 我是否需要在模型或route.php上放一些東西以允許刪除?

方法被調用:

public function destroy($id)
{
    $category = Category::find($id);
    $category->destroy();

    return Redirect::to('/categories');
}

以及形式如下:

{{ Form::open(array('action' => array('CategoryController@destroy', $category->id))) }}

{{ Form::submit('Delete Category', ['class' => 'red_button']) }}

{{ Form::close() }}

類別模型 (它沒有刪除內容的引用,但為防萬一,我添加了它):

class Category extends \Eloquent {
    /*Whitelist what user can enter into form and submit*/
    protected $fillable = [
'name', 'description', 'thumbnail'
    ];

    /*Set up One To Many relationship for users to recipes*/
    public function recipes()
    {
        return $this -> hasMany('Recipe');
    }
}

謝謝你的幫助!

默認情況下,當您通過Form :: open打開一個窗體時,使用的方法是“ POST”,您需要將此方法設置為DELETE。

由於您使用的是資源豐富的路由,所以將destroy操作附加到HTTP DELETE方法。 為了澄清這一點,執行以下命令:

php artisan routes 

此命令向您顯示路由關聯和HTTP方法的詳細列表。

要解決此問題,請嘗試以下操作:

 {{ Form::open(array(
     'action' => array('CategoryController@destroy', $category->id),
     'method' => 'delete')) }} 

我希望為您工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM