有破坏方法的路线吗?

时间:2016-07-03 15:09:31

标签: php laravel routing laravel-5.2

有人能告诉我是否有破坏方法的路线?我正在尝试删除表中的记录,但是当我点击“删除”按钮时,表示我的路线未定义,我认为这就是为什么我的删除操作无效。

  

路由[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>

1 个答案:

答案 0 :(得分:0)

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

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

您没有定义此路线,而是在点击Archive按钮

时尝试点击此路线