Laravel删除foreach循环内的动作

时间:2014-04-21 17:25:33

标签: php laravel laravel-4 foreach

对于显示的每个结果,我希望有相关的删除操作。

foreach循环中,我有:

{{ Form::open(array('route' => array('archive', $alert->id), 'role'=>'form')) }}

<table class="tablesorter table-responsive" id="alerts">
    <thead>
        <tr>
        <th class="header">ID</th>
        <th class="header">Archive</th>
    </tr>
  </thead>
  <tbody>
    @foreach($alert->alerts as $alert)
    <tr>
    <td><button type="submit" class="btn btn-warning" name="archive">Archive</button></td>
  </tr>
  @endforeach
</tbody>
</table>

{{ Form::close() }}

路线:

Route::post('agents/destroy/{id}', 
             array('as' => 'archive', 'uses' => 'AgentsController@postDestroy'));

销毁功能:

public function postDestroy($id)
    {
        $alert  = Alert::find($id);
        $alert->delete();

    }

当我dd($id)时,它会返回null,就好像它没有拿起&#39; id&#39;从提交按钮。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

HTML没有将ID属性传递给PHP,我会按照以下方式执行此操作:

路线:

Route::post('agents/destroy/{id}', array('as' => 'archive', 'uses' => 'blah@postDestroy'));

形式:

{{ Form::open(array('route' => array('archive', $alert->id), 'role'=>'form')) }}
    <td><button type="submit" class="btn btn-warning" name="archive">Archive</button></td>

{{ Form::close() }}

控制器/型号:

public function postDestroy($id)
{
    $alert  = Alert::find($id);
    $alert->delete();
}

据我所知,ID属性仅用于选择DOM中的元素。

修改

更改以下内容:

<table class="tablesorter table-responsive" id="alerts">
    <thead>
        <tr>
            <th class="header">ID</th>
            <th class="header">Archive</th>
        </tr>
    </thead>
    <tbody>
    @foreach($alert->alerts as $alert)
        <tr>
            {{ Form::open(array('route' => array('archive', $alert->id), 'role'=>'form')) }}
            <td><button type="submit" class="btn btn-warning" name="archive">Archive</button></td>
            {{ Form::close() }}
        </tr>
    @endforeach
</tbody>
</table>