0

Welcome ! I'm new to laravel and i have problem. I start my private project and i want to delete query from database using delete button ( simple todo app). When i click it it doesn't delete but only redirect me to id of this query. Dont know what to do. Can someone help me?

Regards tomczas

Destroy function in homecontroller

public function destroy($id)
{
 $todo = Todo::findOrFail($id);
 $todo->delete();
return back();

}

home blade

@foreach($todos as $todo)
          <ul class="todo-list">
            <li>
              <!-- drag handle -->
                  <span class="handle">
                    <i class="fa fa-ellipsis-v"></i>
                    <i class="fa fa-ellipsis-v"></i>
                  </span>
              <!-- checkbox -->
              <input type="checkbox" value="">
              <!-- todo text -->
              <span class="text">{{$todo->tytul}}</span>
              <!-- Emphasis label -->
              <small class="label label-danger"><i class="fa fa-clock-o"></i> {{$todo->czas}}</small>
              <!-- General tools such as edit or delete-->
              <div class="tools">
                <i class="fa fa-edit"></i>
                {{Form::open([ 'method'  => 'DELETE', 'route' => [ 'home.destroy', $todo ] ])}}
                                {{ Form::hidden('id', $todo->id) }}
                                {{Form::button('<i class="fa fa-trash-o"></i>', array('type' => 'submit', 'class' => ''))}}
                            {{ Form::close() }}
                <i class="fa fa-trash-o"></i>
              </div>
            </li>

          </ul>
          @endforeach

3 Answers 3

3

Fix the form:

{{Form::open(['method'  => 'DELETE', 'route' => ['home.destroy', $todo->id]])}}

and no need for:

{{ Form::hidden('id', $todo->id) }}

... laravel does all magick

p.s. make sure You've created resource route to make home controller act as REST controller.

extra, watch this video: https://www.youtube.com/watch?v=6pjPXOwKzJM


another way of doing delete function is that - You can create some route:

Route::get('home/{id}/delete', ['uses' => 'HomeController@destroy', 'as' => 'home.delete']);

and in this case no need for form:

<div class="tools">
  <i class="fa fa-edit"></i>
  <a href="{{route('home.delete', $todo->id)}}?{{time()}}">
    <i class="fa fa-trash-o"></i>
  </a>        
</div>



p.s. after reading Your routes I see also core problem:

Route::resource('home', 'HomeController@index');

should be (read this):

Route::resource('home', 'HomeController');
Sign up to request clarification or add additional context in comments.

4 Comments

I have this Route::resource('home', 'HomeController@index'); and it still redirects me to mypath/1 where 1 is ID task which i want to delete
@tomczas I've updated my answer. So I'm not depending on "magics" of frameworks and I'm defining my routes exacly, and using "another way of ding" example in my answer.
NotFoundHttpException in RouteCollection.php line 161:
Route::get('/', function () { return view('welcome'); }); Route::resource('home', 'HomeController@index'); Route::get('/home/{$id}/delete', ['use' => 'HomeController@destroy', 'as' => 'home.delete']); Route::get('logout', array('uses' => 'HomeController@doLogout')); Route::auth();
-1

Ok second idea is working great but little mistake in spelling:

should be:

Route::get('/home/{id}/delete', ['uses' => 'HomeController@destroy', 'as' => 'home.delete']);

so {id} not :id and uses not use Thank u very much for help !

1 Comment

I've fixed it in my answer (: I'm not using LV so much last year so forgot how to set the {id} and :id is from expressjs (;
-1

in a new version of laravel 5.6 , laravel suggest to use code below to delete one row

<form method="post" action={{ route('yourControllerDeleteMethodName'), ['yourRowName'=> $yourRowName->slug] }}> <input type="hidden" value="DELETE"/> <button type="submit">click to delete</button> </form>

this work when you use slug to get row if you use id to get row just write $id instead of slug.

hope to work for you:)\

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.