Laravel multiple delete checkbox

Hi, I only want to delete checked tasks. So to achieve this goal, I am using an array variable for the name attribute for my checkbox.

At the moment I have this:

<div class="dropdown">
    <button type="button" class="btn btn-danger dropdown-toggle selDelete" data-toggle="dropdown" id="deleteTask">
        <input id="check1" name="deleteCheckedTask[]" type="checkbox" class="check" value="{{$Task->id}}">
        <span class="caret-hover caret"></span>
    </button>
    <ul class="dropdown-menu" aria-labelledby="selDelete" role="menu">
        <li><a class="deleteTask" href= "{{ route('user.tasks.destroy',array( $Task->id )) }}" data-method="delete" >Deletey</a></li>
    </ul>   
</div>

      

What looks like in the browser: link: http://i.imgur.com/Sv8l3OX.png

But this will only send the id of 1 task to be deleted.

I also tried this:

<div class="dropdown">
    <button type="button" class="btn btn-danger dropdown-toggle selDelete" data-toggle="dropdown" id="deleteTask">
        <input id="check1" name="deleteCheckedTask[]" type="checkbox" class="check" value="{{$Task->id}}">
        <span class="caret-hover caret"></span>
    </button>
    <ul class="dropdown-menu" aria-labelledby="selDelete" role="menu">
        <li><a class="deleteTask" href= "{{ route('user.tasks.destroy',array( $deleteCheckedTask)) }}" data-method="delete" >Deletey</a></li>
    </ul>   
</div>

      

But then I get undefined error, here is a screenshot of the error: http://i.imgur.com/QW5De7y.png

And this is how now my delete () from TasksController.php looks like this:

public function destroy($id){
    //$deleteCheckedTasks=array();
    $deleteCheckedTasks=Input::get('deleteCheckedTask');
    //die(print_r(Input::get('deleteCheckedTask')));
    foreach ($deleteCheckedTasks as $key => $value ) {
        print 'val: '. $key ;
    }


    //$row= DB::table('ordertasks')->where('id', $id)->first();
    //Task::destroy($id);
    //Task::destroy($row->id_task);
    //Ordertask::destroy($id);
    //delete werkt niet op vu nam zijn pc
    if (Request::path()=='api/v1/tasks/'.$id) {
        return Response::json(array(
        'error' => false,
        'tasks' => 'task deleted'),
        200
    );

    }

    return Redirect::route('user.tasks.index');
}

      

I know how I can do this with jQuery, but I happily want to do it with Laravel. Can someone help me please? I think I am very close.

+3


source to share


1 answer


I think you can do it in the form

html with blade syntax

<form method="POST" action="/destroy">
    {{ Form::token() }}
    @foreach($tasks as $t)
        <label>
            <input type="checkbox" name="checked[]" value="{{ $t->id }}">
            {{ $t->name }}
        </label>
    @endforeach
    <button type="submit">Submit!</button>
</form>

      

route



Route::post('/destroy', array('before' => 'csrf', 'uses' => 'TaskController@handleDestroy'));

      

TaskController.php

<?php

    class TaskController extends BaseController {
        public function handleDestroy() {
            $checked = Input::only('checked')['checked'];
            // Do whatever you want with this array
        }
    }

      

+2


source







All Articles