Laravel 4 - 从数据库中删除记录

时间:2014-05-20 12:43:43

标签: laravel laravel-4

再一次,我很难过。

我有一个信件表,每个都附有以下链接

<a href="">Delete {{ $letter->lettername }}</a>

从文档我可以看到我必须从我的routes.php

运行以下内容
$letter = Letter::find(1);
$letter->delete();

我的问题是,我需要在href中输入什么来将字母ID传递给路径文件,然后如何将其传递给find()参数?

我会做这样的事吗

<a href="letters/delete/IDHERE">Delete {{ $letter->lettername }}</a>

如果是这样,我如何将该ID放入查找参数中。

我很困惑。

非常感谢任何帮助

干杯

我的路线文件如下:

Route::get('letters', array(
'before' => 'auth|userdetail',
function()
{

    // Grab the letters, if any, for this user
    $letters = Letters::forUser(Auth::user())->get();

    $data = [
        'letters' => $letters
    ];

    return View::make('letters', $data);

}
));

1 个答案:

答案 0 :(得分:4)

你可以这样做

<a href="letters/delete/{{ $letter->id }}">Delete {{ $letter->lettername }}</a>

But it would be better if you have a named route

<a href="{{ URL::route('letters.delete', $letter->id) }}">Delete {{ $letter->lettername }}</a>

然后在你的路线

Route::get('letters/delete/{id}', array('as' => 'letters.delete', 'before' => 'auth|userdetail', function($id)
{
    echo ("You want to delete letter id: " . $id . " from the system");
    // Put your delete code here
}