Laravel删除foreach循环内的动作

时间:2022-10-26 09:16:39

For each result displayed, I want there to be an associated delete action.

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

Inside the foreach loop I have:

在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:

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

destroy function:

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

    }

When I dd($id), it returns null, as if it's not picking up the 'id' from the submit button.

当我dd($ id)时,它返回null,就好像它没有从提交按钮中获取'id'。

Any help would be hugely appreciated.

任何帮助将非常感激。

1 个解决方案

#1


0  

HTML doesn't pass the ID attribute to PHP, I would do what you're trying to do like this:

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

Route:

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

Form:

{{ 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() }}

Controller/Model:

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

As far as I am aware ID attributes are only really used for selecting elements within the DOM.

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

Edit:

Change the following:

更改以下内容:

<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>

#1


0  

HTML doesn't pass the ID attribute to PHP, I would do what you're trying to do like this:

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

Route:

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

Form:

{{ 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() }}

Controller/Model:

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

As far as I am aware ID attributes are only really used for selecting elements within the DOM.

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

Edit:

Change the following:

更改以下内容:

<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>