Laravel:在函数控制器上为foreach()提供的参数无效

时间:2017-08-28 10:00:14

标签: php ajax laravel-5

我知道还有其他问题有相同的标题,但我无法找到错误。

我的观点如下:

<table class="table">
                <thead style="color:white">
                  <tr>
                    <th><a href="{{route('admin.projects.order', ['field' => 'id','order' => 'asc'])}}"><span class="glyphicon glyphicon-arrow-up" id="orderByIdAsc"></span></a>ID<a href="{{route('admin.projects.order', ['field' => 'id','order' => 'desc'])}}"><span class="glyphicon glyphicon-arrow-down" id="orderByIdDesc"></span></a></th>
                    <th><a href="{{route('admin.projects.order', ['field' => 'slug','order' => 'asc'])}}"><span class="glyphicon glyphicon-arrow-up" id="orderBySlugAsc"></span></a>SLUG<a href="{{route('admin.projects.order',['field' => 'slug','order' => 'desc'])}}"><span class="glyphicon glyphicon-arrow-down" id="orderBySlugDown"></span></a></th>
                    <th><a href="{{route('admin.projects.order', ['field' => 'order','order' => 'asc'])}}"><span class="glyphicon glyphicon-arrow-up" id="orderByOrderAsc"></span></a>ORDER<a href="{{route('admin.projects.order', ['field' => 'order','order' => 'desc'])}}"><span class="glyphicon glyphicon-arrow-down" id="orderByOrderDesc"></span></a></th>
                    <th><a href="{{route('admin.projects.order', ['field' => 'public','order' => 'asc'])}}"><span class="glyphicon glyphicon-arrow-up" id="orderByPublicAsc"></span></a>PUBLIC<a href="{{route('admin.projects.order', ['field' => 'public','order' => 'desc'])}}"><span class="glyphicon glyphicon-arrow-down" id="orderByPublicDesc"></span></a></th>
                    <th><span class="glyphicon glyphicon-cog"></span></th>
                  </tr>
                </thead>
                <tbody style="color:white">
                  @foreach ($projects as $key => $project)
                    <tr id="{{$project->id}}">
                      <td>{{$project->id}}</td>
                      <td>{{$project->slug}}</td>
                      <td>{{$project->order}}</td>
                      <td>{{$project->public}}</td>
                      <td><a href="{{ route('admin.projects.show', $project->id)}}" class="btn btn-info btn-sm">View</a> <a href="{{ route('admin.project.edit', $project->id)}}" class="btn btn-success btn-sm">Edit</a></td>
                    </tr>
                  @endforeach
                </tbody>
</table>

Ajax代码

$("#tabs").tabs();

$("tbody").sortable({
    items: "> tr",
    appendTo: "parent",
    helper: "clone"
}).disableSelection();

$("#tabs ul li a").droppable({
    hoverClass: "drophover",
    tolerance: "pointer",
    drop: function(e, ui) {
        var tabdiv = $(this).attr("href");
        $(tabdiv + " table tr:last").after("<tr>" + ui.draggable.html() + "</tr>");
        ui.draggable.remove();
    }
});

$("tbody").sortable({
    items: "> tr",
    appendTo: "parent",
    helper: "clone",
    update: function( event, ui ) {
        let newOrder = $(this).sortable('toArray').toString();
        $.ajax({
            url:'/admin/projects/updateOrder',
            type:'POST',
            data: newOrder
        })
       .done(function( msg ) {
            // render table with new order?
       });
    }
}).disableSelection();

关于web.php的网址

Route::post('projects/updateOrder', ['uses' => 'AdminController@updateOrder', 'as' => 'admin.projects.updateOrder']);

控制器错误,错误

public function updateOrder(Request $request){
        $ids = $request->ids;
        $caseQuery = 'CASE id ';
        foreach ( $ids as $order => $id) {
            $caseQuery .= "WHEN $id THEN $order";
        }
        $caseQuery .= 'END CASE';

        DB::table('projects')->whereIn('id',$ids)
                             ->update(['order' => '$caseQuery']);
    }

如果我退回$ids,请不要做任何事情。 如果我检查传递的数据是否通过了所有Id。

我在尝试什么?当我拖动时,获取ids的新值&#39; n&#39;放下并用ajax命令。

如果有任何问题可以随意提问。

如何修复错误?非常感谢。

2 个答案:

答案 0 :(得分:0)

我在您的视图或ajax代码中看不到任何名为ids的内容。我认为问题与此有关。 据我所知,jQuery UI可排序toArray方法只返回一个ID数组。所以你应该用Ajax代码自己命名数组:

$("tbody").sortable({
items: "> tr",
appendTo: "parent",
helper: "clone",
update: function( event, ui ) {
    let newOrder = $(this).sortable('toArray').toString();
    $.ajax({
        url:'/admin/projects/updateOrder',
        type:'POST',
        data: { ids: newOrder }
    })
   .done(function( msg ) {
        // render table with new order?
   });
}
}).disableSelection();

注意&#34; {ids: newOrder }&#34;一部分。

我没有足够的时间来测试它,但您可以尝试给我一个反馈。祝你有愉快的一天!

答案 1 :(得分:0)

首先尝试使用以下

打印完整的帖子数据
 public function updateOrder(Request $request){
    print_r($request->all());
     ...........
 }

并相应地获取变量中的所有id,然后在将其传递给whereIn子句之前使用explode将其转换为数组。

使用此

$ids = explode(',' , $request->ids);

由于whereIn子句将第二个参数作为数组而不是字符串。