Laravel 5.1分类排序

496 查看

其实排序很简单,就是将需要排序的ID和排序的值一一对应更新就OK了。

模板页面(index.blade.php)

{!! Form::open( array('url' => 'admin/category/sort/', 'method' => 'put') ) !!}

@foreach($categories as $category)
{{--此处省略一些字段--}}
<td><input class="sort" type="text" name="sort[]" value="{{$user_category ->sort}}"></td>
<input type="hidden" name="id[]" value="{{$user_category->id}}">

@endforeach


<div class="btn-group pull-left btn-sort">
    <button class="btn btn-block btn-primary" type="submit">排序</button>
</div>

{!!Form::close()!!}

控制器:

public function sort(Request $request)
    {
        $sorts = Input::get('sort');
        $ids = Input::get('id');

        foreach($sorts as $index => $sort){
            $id = $ids[$index];
            $st = Category::find($id);
            $st->update([
                'sort' => $sort
            ]);
        }
        return redirect('admin/category')->with('message', '排序成功!');
    }

路由:

Route::put('category/sort','CategoryController@sort');

如图:

原文:http://note.mango.im/article/29