【问题标题】:jquery sortable with laravel doesn't work可使用 laravel 排序的 jquery 不起作用
【发布时间】:2018-03-04 18:19:17
【问题描述】:

我对 laravel 中的可排序菜单有疑问。如果我加载我的页面,它会给我一个致命错误,上面写着“在 web.php 第 49 行中找不到类'输入'”。

这是第 49 行的内容:“$itemID = Input::get('itemID');”。下面你会看到整个代码块

Route::get('/custom',function(){
    $menu = DB::table('orders')->orderBy('order','ASC')->get();
    $itemID = Input::get('itemID');
    $itemIndex = Input::get('itemIndex');

    foreach($menu as $value){
        return DB::table('orders')->where('menu_id','=',$itemID)->update(array('order'=> $itemIndex));
    }});

这是我的 jquery:

$('document').ready(function(){
    $(function(){
      $("#menu").sortable({
        stop: function(){
          $.map($(this).find('li'), function(el) {
            var itemID = el.id;
            var itemIndex = $(el).index();
            $.ajax({
              url:'{{URL::to("custom")}}',
              type:'GET',
              dataType:'json',
              data: {itemID:itemID, itemIndex: itemIndex},
            })
          });
        }
      });
    });

    console.log(itemID);

});

这是我的路线文件:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/



Route::get('/', function () {
    return view('home');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::get('/custom', function(){
    return view('custom');
});

Route::get('/custom-menu', function(){
    return view('custom');
});


// function to view menu which are in order
Route::get('/', function () {
    $menu = DB::table('orders')->orderBy('order','ASC')->get();
    return view('custom-menu',['menus'=>$menu]);
});


// To view Menu That are in database
Route::get('custom',function(){
    $menu = DB::table('orders')->orderBy('order','ASC')->get();
    return view('custom',['menus'=>$menu]);
});

// Function to order menus
Route::get('/custom',function(){
        $menu = DB::table('orders')->orderBy('order','ASC')->get();
        $itemID = Input::get('itemID');
        $itemIndex = Input::get('itemIndex');

        foreach($menu as $value){
            return DB::table('orders')->where('menu_id','=',$itemID)->update(array('order'=> $itemIndex));
        }
});

有人知道我该如何解决这个错误吗?

【问题讨论】:

  • 尝试使用输入法;在 web.php 中

标签: laravel jquery-ui jquery-ui-sortable


【解决方案1】:
use Illuminate\Support\Facades\Input;

或添加

'Input' => Illuminate\Support\Facades\Input::class,

在 config/app.php 中的别名 和 放置use Input;在web.php中

【讨论】:

  • 如果我这样做,它会说“使用非复合名称'输入'的使用语句无效”
  • 如果我输入“使用 Illuminate\Support\Facades\Input;”在我的 web.php 文件中,它在我的页面上加载一个零
  • 尝试使用 Illuminate\Support\Facades\Input::get('itemIndex');
  • 如果我这样做我得到:解析错误,期待','' or ';''
【解决方案2】:

如果你想从你的路由文件中访问请求,use $request:

use Illuminate\Http\Request;

Route::get('/custom',function(Request $request) {
    $itemID = $request->itemID;
    // ...

更新

这段代码还有一个问题:

foreach($menu as $value){
    return DB::table('orders')->where('menu_id','=',$itemID)->update(array('order'=> $itemIndex));
}});

这将开始遍历您在第一个查询 ($menu = DB::table ...) 中找到的每个 $menu,但它在第一个查询之后是 returns。这意味着它永远不会更新您的所有项目,只会更新第一个。

您在评论中说it returns 0。来自the Laravel docs

应该使用 update 方法来更新数据库中的现有记录。将返回受语句影响的行数

因此,如果您正在查看发送给您的 AJAX 调用的响应,并且您看到 0,这意味着对于要更新的​​第一个 $menu,实际上没有更新任何记录。

要更新所有记录,请尝试从循环中删除返回,这样它就完成了:

foreach($menu as $value){
    DB::table('orders')->where('menu_id','=',$itemID)->update(array('order'=> $itemIndex));
}});

return;

【讨论】:

  • 它一直返回一个零
  • @CorneliusVanErkelens 什么返回 0?我测试了答案中的代码,它可以工作。
  • @CorneliusVanErkelens 我找到了你可能的意思,并更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-01
相关资源
最近更新 更多