-1

I am a newbie in Laravel programming. I have searched so many resources about delete functions but it doesn't work. When I click my delete button, it only shows 404 NOT FOUND. I am using Laravel 7.0. Hopefully, you can help me resolve it. Thank you! enter image description here

Here

/**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $student = Student::find($id);
        $student->delete();
        return redirect('/');
    }

Here is my Route:

Route::get('/delete/{id}','StudentController@destroy');

Here is my View:

<table class="table thead-light">
  <thead>
    <tr>
      <th scope="col">ID</th>
      <th scope="col">Mã sinh viên</th>
      <th scope="col">Khối</th>
      <th scope="col">Tên lớp</th>
      <th scope="col">Họ và Tên</th>
      <th scope="col">Thầy/Cô giáo chủ nhiệm</th>
      <th scope="col">Tính năng</th>
    </tr>
  </thead>
  <tbody>
    @foreach($students as $student)
    <tr>
        <th scope="row">{{ $student->id }}</th>
        <td>{{ $student->student_id }}</td>
        <td>{{ $student->grade }}</td>
        <td>{{ $student->class }}</td>
        <td>{{ $student->fullname }}</td>
        <td>{{ $student->head_teacher }}</td>
        <td id="functions">           
          <a href="#" class="btn btn-info">Xem hồ sơ</a> 
            <a href="{{ url('/edit/'.$student->id) }}" class="btn btn-warning">Sửa</a>
            <a href='delete/{{ $student->id }}' class="btn btn-danger">Xóa</a>
        </td>
    </tr>    
    @endforeach  
    </tbody>
</table>
Hòa Đỗ
  • 31
  • 6

3 Answers3

0

Another route might be overriding your delete route.

For delete operations, you sould be using a HTTP Delete request instead of Get.

Route::delete('/delete/{id}','StudentController@destroy');
<form action="/delete/{{ $student->id }}" method="POST">
    @csrf
    @method('DELETE')
    <button type="submit">Delete</button>
</form>
P. K. Tharindu
  • 1,696
  • 3
  • 12
  • 27
0

Your link for deleting is incorrect. You should use named routes.

For your Route

Route::get('/delete/{id}','StudentController@destroy')->name('delete.student');

For the link in the View

<a href='{{ route('delete.student', $student->id) }}' class="btn btn-danger">Xóa</a>

Consider changing your edit link as well. Try not to use url() for links, use named routes.

migsAV
  • 106
  • 3
0

Well, I have found the solution to my problem. I have just made reference to this article: 404 Not Found, but route exist in Laravel 5.4. In a nutshell, I should use "php artisan route:clear" and "php artisan route:list".

Hòa Đỗ
  • 31
  • 6