2017-09-23 1 views
0

laravel에서 정렬 가능한 메뉴에 대한 질문이 있습니다. 내 페이지를로드하면 "Class 'Input'이 web.php 줄 49에 없습니다"라는 치명적인 오류가 발생합니다.jquery가 laravel로 정렬 할 수 없습니다.

이것은 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

시도를 사용 입력; in web.php – RamAnji

답변

0
use Illuminate\Support\Facades\Input; 

또는 추가

'입력'=>를 분명히 \ 지원 \ 외관 \ 입력 : 클래스,

설정에서

/app.php 별칭 및 장소 use Input; web.php에서

+0

"복합체 이름이 아닌 입력 문 'Input'은 아무런 효과가 없습니다." –

+0

"Illuminate \ Support \ Facades \ Input; 내 web.php 파일에서 내 페이지에 0을로드합니다. –

+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 = DB::table ...)에서 찾을 각 $menu을 반복하기 시작할 것입니다,하지만 그것은 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을 계속 반환합니다. –

+0

@CorneliusVanErkelens 0을 반환하는 값은 무엇입니까? 내 대답에 코드를 테스트하고 그것을 작동합니다. –

+0

@CorneliusVanErkelens 나는 아마도 당신이 의미하는 것을 발견하고 나의 대답을 업데이트했습니다. –

관련 문제