Home > Mobile >  laravel - Delete method not working in laravel 8
laravel - Delete method not working in laravel 8

Time:05-25

Hello I'm still a beginner at laravel, I got a problem on the delete method.

this my controller

public function destroy($id)
    {
        $home = Home::find($id);
        $home -> delete();
        return back();
    }

this my balde

<div >
 <form action="{{ route('home.destroy', $home->id) }}" method="POST">
  @csrf
  @method('DELETE')
    <a href="">
    <i  style="width: 10px; height:10px;"></i>
    </a>
 </form>
</div>

this my route

Route::resource('home', 'App\Http\Controllers\TodosController')->middleware('auth');

When I press the delete button, why doesn't it work? Where is the problem? pls, help.

CodePudding user response:

At the first you need to get what you want to delete this is an example for you it would be your controller:

///REPLACE Crud with your MODEL////

   public function destroy(Request $request,$id){
        $data = Crud::findOrFail($id);
        $data->delete();
        return redirect('YOUR_HOME_PAGE')->with('success', 'User is successfully deleted');
}

this is your blade:

<td>
                
                    <form action="{{ route('home.destroy', $row->id) }}" method="post">
                                                
                    @csrf
                    @method('DELETE')
                    <button type="submit"   data-toggle="tooltip" title='Delete'>Delete</button>
                </form>
            </td>
        </tr>


 
  • Related