2

I'm new to deleting multiple checkboxes so i need a little help. I've been searching the web for like hours but i can't fix my problem.

I have a todo list and there are some todo's showing from the database on my webpage.How can i delete all selected checkboxes in one click?

I'm using Laravel 5.4

ToDoController:

public function destroy()
   {
    $checked = Request::input('checked')['checked'];
    $checked->delete();
   }

Blade:

@foreach ($todo as $todoresult)

            <form method="get" action="/todo/destroy/">

          <div class="checkbox">
           <label><input type="checkbox" value="{{$todoresult->id}}" name="checked[]">{{$todoresult->todoinfo}}</label><br />

          </div> 

            @endforeach

            </div>
            <div class="panel-footer">

            <button class="btn btn-success" type="submit">Verwerken</button>

            </form>

Route:

Route::get('/todo/destroy/', 'ToDoController@destroy');

How would i do this? And how would my routing be like?

2 Answers 2

4

Something like the following would probably work:

public function destroy() {
   $checked = Request::input('checked',[]);
   foreach ($checked as $id) {
        Todo::where("id",$id)->delete(); //Assuming you have a Todo model. 
   }
   //Or as @Alex suggested 
   Todo::whereIn($checked)->delete();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Todo::whereIn('id', $checked)->delete()
Hi, thanks for your answer. When i use this i get this in the url: /todo/destroy?checked%5B%5D=26&checked%5B%5D=27. 26 and 27 are the correct ID. But how can i route this?
4

You don't need to loop query. You can use WhereIn to delete multiple ids

   public function destroy() {
       $checked = Request::input('checked',[]);
      Todo::whereIn("id",$checked)->delete(); 

}

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.