lundi 28 août 2017

Laravel: Invalid argument supplied for foreach() on function controller

I know there are others questions with same title, but I can't find the error.

My view looks like here:

<table class="table">
                <thead style="color:white">
                  <tr>
                    <th><a href=""><span class="glyphicon glyphicon-arrow-up" id="orderByIdAsc"></span></a>ID<a href=""><span class="glyphicon glyphicon-arrow-down" id="orderByIdDesc"></span></a></th>
                    <th><a href=""><span class="glyphicon glyphicon-arrow-up" id="orderBySlugAsc"></span></a>SLUG<a href=""><span class="glyphicon glyphicon-arrow-down" id="orderBySlugDown"></span></a></th>
                    <th><a href=""><span class="glyphicon glyphicon-arrow-up" id="orderByOrderAsc"></span></a>ORDER<a href=""><span class="glyphicon glyphicon-arrow-down" id="orderByOrderDesc"></span></a></th>
                    <th><a href=""><span class="glyphicon glyphicon-arrow-up" id="orderByPublicAsc"></span></a>PUBLIC<a href=""><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="">
                      <td></td>
                      <td></td>
                      <td></td>
                      <td></td>
                      <td><a href="" class="btn btn-info btn-sm">View</a> <a href="" class="btn btn-success btn-sm">Edit</a></td>
                    </tr>
                  @endforeach
                </tbody>
</table>

Ajax code

$("#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();

Url on web.php:

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

Controller error with the error

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

If I put a return of $ids don't get anything. If I check the data passed pass all the Id's.

What i'm trying? Get the new values of ids when I make a drag 'n' drop and order it with ajax.

If have any question feel free to ask it.

How to fix the error? Thanks a lot.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire