Francisunoxx Francisunoxx - 5 months ago 8
PHP Question

Is there a route for destroy method?

Can anyone tell me if there's a route for destroy method? I'm trying to delete my records in table but when I hit the Delete button is says my route is not define I think that's why my delete action won't work.


Route [result.destroyEmployee] not defined. (View: C:\Users\JohnFrancis\LaravelFrancis\resources\views\account\search.blade.php)


Routes

//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',
]);


I passed the
id
in the edit routes so it will identify who id is currently in action.

Controller:

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

Answer

Add this route in your routes.php. I don't encourage delete operation in a GET request, but since you have the operation defined on anchor tag, it will be a GET request.

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

You don't have this route defined and you are trying to hit it on clicking Archive button