Laravel多个删除复选框

时间:2014-12-08 23:41:39

标签: php laravel laravel-routing

很高兴我想只删除已检查的任务。所以要实现这一点,我使用数组变量作为我的复选框的name属性。

目前我有这个:

<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>

在浏览器中看起来像这样: 链接:http://i.imgur.com/Sv8l3OX.png

但是这只会发送1个将要删除的任务的ID。

我也试过这个:

<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>

但后来我得到错误未定义变量,这里是错误的屏幕截图: http://i.imgur.com/QW5De7y.png

这就是我在TasksController.php中的delete()目前的样子:

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');
}

我知道如何用jQuery做到这一点,但我很乐意用Laravel来做这件事。有谁可以帮助我吗?我认为我非常接近。

1 个答案:

答案 0 :(得分:2)

我认为你可以用表格

来做 带有刀片语法的

html

<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::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
        }
    }