【问题标题】:Is there a route for destroy method?是否有销毁方法的路线?
【发布时间】:2016-11-05 09:26:12
【问题描述】:

谁能告诉我是否有销毁方法的路线?我正在尝试删除表中的记录,但是当我点击删除按钮时说我的路线未定义,我认为这就是我的删除操作不起作用的原因。

路线 [result.destroyEmployee] 未定义。 (查看:C:\Users\JohnFrancis\LaravelFrancis\resources\views\account\search.blade.php)

路线

//READ
Route::get('/search',
[
    'uses' => '\App\Http\Controllers\AccountController@getEmployee',
    'as' => 'account.search',
]);


//EDIT
Route::get('/edit/{id}',
[
    'uses' => '\App\Http\Controllers\AccountController@editEmployee',
    'as' => 'account.edit',
]);

我在编辑路由中传递了id,因此它会识别出当前正在行动的 id。

控制器:

//READ
public function getEmployee()
{
    $result = DB::table('users')->get();

    return view ('account.search')->with('result', $result);
}


//EDIT
public function editEmployee($id)
{
    $result = User::find($id);

                                        //key     //value
    return view ('account.edit')->with('result', $result);
}

//DELETE
public function destroyEmployee($id)
{
    $result = User::destroy($id);

    return redirect()->route('account.search');
}

search.blade.php

@foreach ($result as $row)
    <tr class = "success">
        <td>{{ $row->id }}</td>
        <td>{{ $row->first_name }}</td>
        <td>{{ $row->last_name }}</td>
        <td>{{ $row->middle_name }}</td>
        <td>{{ $row->email }}</td>
        <td>{{ $row->username }}</td>
        <td>
            <a href = "{{ route ('account.edit', $row->id) }}"><button type = "submit" class = "btn btn-warning">Edit</button></a>

            <a href = "{{ route ('result.destroyEmployee', $row->id) }}"><button type = "submit" class = "btn btn-danger">Delete</button></a>
        </td>
    </tr>
@endforeach

edit.blade.php

<form class = "form-vertical" role = "form" method = "post" action = "{{ route ('account.edit', $result->id) }}">

<div class = "form-group">

    <label for = "email" class = "control-label">Email Address</label>
    <input type = "text" name = "email" class = "form-control" value = "{{ $result->email }}">

</div>

<div class = "form-group">

    <label for = "username" class = "control-label">Username</label>
    <input type = "text" name = "username" class = "form-control" value = "{{ $result->username }}">

</div>

<div class = "form-group">

    <button type = "submit" class = "btn btn-success">Save</button> 

</div>

<input type = "hidden" name = "id" value = "{{ $result->id }}">
<input type = "hidden" name = "_token" value = "{{ Session::token() }}">

</form>

【问题讨论】:

  • 好吧,我没有看到任何指向destroyEmployee 方法的路线。这就是错误所说的。
  • @VishalSh 我需要为此创建路线吗?

标签: php laravel routing laravel-5.2


【解决方案1】:

在您的routes.php 中添加这条路线。我不鼓励在 GET 请求中进行删除操作,但是由于您在锚标记上定义了操作,因此它将是一个 GET 请求。

//DELETE
Route::get('/destroy/{id}',
[
    'uses' => 'AccountController@destroyEmployee',
    'as' => 'result.destroyEmployee',
]);

你没有定义这条路线,你试图点击Archive按钮来点击它

【讨论】:

  • 为什么不鼓励使用 GET 方法?你能提供一些POST方法的例子吗?
  • 该操作导致数据库中的 DELETE,此操作的理想 REST 调用是 DELETE 请求。您必须将锚标记更改为按钮标记并添加onclick 方法来为除 GET 之外的任何请求发送 AJAX 调用。 (不过,您始终可以使用表单)
  • 所以如果操作没有在标签内分配。假设该操作在按钮内,所以这将是一个 POST 请求,并且仍然在数据库中删除,对吗?如果我错了,请纠正我。
  • 除非您将 ajax 调用配置为发出 POST 请求,否则它不会是 POST 请求。将&lt;a&gt; 简单地替换为&lt;button&gt; 根本不会发出请求。您需要将 onclick 函数绑定到它并在该方法中构建您的 AJAX 请求。
  • 根据我阅读的内容,POST 比 GET 请求更安全。那么我可以使用销毁方法使用 POST 请求吗?如果可以使用我需要做哪些更改?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-10
  • 2021-07-09
  • 1970-01-01
  • 2010-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多