2017-09-23 64 views
0

我有一个关于laravel中可排序菜单的问题。如果我加载我的网页,它给了我一个致命的错误,说“Class'Input'没有在web.php第49行”中找到。与laravel排序的jquery不起作用

这是第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', '[email protected]')->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)); 
     } 
}); 

是否有人知道我可以解决这个错误?

+0

尝试使用输入;在web.php – RamAnji

回答

0
use Illuminate\Support\Facades\Input; 

或添加

'输入'=>照亮\支持\外墙\输入::类,

在配置

/app.php别名 和 地方use Input;在web.php

+0

如果我这样说它说:“使用声明与非复合名称'输入'没有任何影响” –

+0

如果我把“使用Illuminate \ Support \ Facades \ Input;”在我的web.php文件中,它会在我的页面上加载一个零 –

+0

尝试使用Illuminate \ Support \ Facades \ Input :: get('itemIndex'); – RamAnji

0

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

use Illuminate\Http\Request; 

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

UPDATE

有这码另一个问题:

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

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

在你的评论中,你说it returns 0。从the Laravel docs

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

所以,如果你正在寻找发送到您的AJAX调用的响应,并且看到0,这意味着第一$menu进行更新,没有记录实际上已更新。

要更新所有的记录,尝试只去除循环返回,所以它完成:

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

return; 
+0

它不断返回一个零 –

+0

@CorneliusVanErkelens什么返回0?我在我的答案中测试了代码,它工作正常。 –

+0

@CorneliusVanErkelens我找到了你可能的意思,并更新了我的答案。 –