发现不在laravel工作

时间:2016-05-23 04:25:21

标签: javascript php laravel sorting

我试图对行的顺序进行排序。但是当我试图在数据库中找到它时它不起作用。 $ products = Product :: find($ productsIds [$ i]);没有找到值..如果有人能找到错误,那将是grat帮助。谢谢你

<form method="post" action="{{ url('/ordering') }}" id="categoryOrderingForm">
            <input type="hidden" value="{{ csrf_token() }}" name="_token" />
            <input type="hidden" name="order" id="rowOrder" /> 
        </form>
javascript is
<script>
$(function() {
    $( "#sortable" ).sortable({
      placeholder: "ui-state-highlight"
    });
    $( "#sortable" ).disableSelection();

    $(".saveOrder").on("click", function() {
        var selectedLanguage = new Array();
        $('#sortable .sortable-row').each(function() {
            selectedLanguage.push($(this).attr("id"));
        });
        $("#rowOrder").val(selectedLanguage);
        $("#categoryOrderingForm").submit();
    });
});
</script>

,控制器是

    public function postOrdering()
    {
      $input = Request::all();
     // dd($input);
      if(isset($input["order"]) && count($input["order"]))
      {
          $productsIds = explode(",", $input["order"]);
         // dd($productsIds);
          if($productsIds && count($productsIds))
          {    
            //dd(count($productsIds));          
              for($i=0; $i< count($productsIds); $i++) 
              { 
                //dd(count($productsIds[$i]));    
                //$products=Product::all();

                 // $products = Product::where("id", $productsIds[$i])->first();
                   $products = Product::find($productsIds[$i]);
                    //echo $products ;
                    //dd($products);
                  if( !is_null($products))
                  {
                    $products->products_order = $i;
                    $products->save();
                  }
              }
          }
      }

      return redirect()->back();
  }

1 个答案:

答案 0 :(得分:0)

试试这个:

 $products = Product::find($productsIds[$i])->get();

OR

 $products = Product::find($productsIds[$i])->all();

仅限单曲:

$products = Product::find($productsIds[$i])->one();