2017-08-29 24 views
1

この質問は既に尋ねられていますが、良い答えが見つかりません。Mysql:一般的なエラーの整数値が正しくありません

ビューは次のようになります。

@foreach ($projects as $key => $project) 
        <tr id="{{$project->id}}"> 
         <td>{{$project->id}}</td> 
         <td>{{$project->slug}}</td> 
         <td>{{$project->order}}</td> 
         <td>{{$project->public}}</td> 
         <td><a href="{{ route('admin.projects.show', $project->id)}}" class="btn btn-info btn-sm">View</a> <a href="{{ route('admin.project.edit', $project->id)}}" class="btn btn-success btn-sm">Edit</a></td> 
        </tr> 
@endforeach 

Ajaxコードは次のようになります。

$("tbody").sortable({ 
    items: "> tr", 
    appendTo: "parent", 
    helper: "clone", 
    update: function(event, ui) { 
     let newOrder = $(this).sortable('toArray'); 
     $.ajax({ 
      type: "POST", 
      url:'/admin/projects/updateOrder', 
      data: {ids: newOrder} 
     }) 
     .done(function(msg) { 
      // render table with new order? 
     }); 
    } 
}).disableSelection(); 

コントローラの機能は次のようになります。

public function updateOrder(Request $request) 
    { 
     $ids = $request->ids; 
     // en el array ids, los "keys" serían el orden, los cuales puedes modificar según lo que requieras 

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

エラー:

SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'CASE 
id WHEN 2 THEN 0 WHEN 3 THEN 1 WHEN 1 THEN 2 WHEN 4 THEN 3 WHEN 5 
THEN 4 WHEN 6 THEN 5 WHEN 7 THEN 6 WHEN 8 THEN 7 WHEN 9 T' for column 
'order' at row 1 (SQL: update `projects` set `order` = CASE id WHEN 2 
THEN 0 WHEN 3 THEN 1 WHEN 1 THEN 2 WHEN 4 THEN 3 WHEN 5 THEN 4 WHEN 6 
THEN 5 WHEN 7 THEN 6 WHEN 8 THEN 7 WHEN 9 THEN 8 WHEN 10 THEN 9 WHEN 
11 THEN 10 WHEN 12 THEN 11 WHEN 13 THEN 12 WHEN 14 THEN 13 WHEN 15 
THEN 14 WHEN 16 THEN 15 WHEN 17 THEN 16 WHEN 18 THEN 17 WHEN 19 THEN 
18 WHEN 20 THEN 19 WHEN 21 THEN 20 WHEN 22 THEN 21 WHEN 23 THEN 22 
WHEN 24 THEN 23 WHEN 25 THEN 24 WHEN 26 THEN 25 WHEN 27 THEN 26 WHEN 
28 THEN 27 WHEN 29 THEN 28 END CASE where `id` in (2, 3, 1, 4, 5, 6, 
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 
25, 26, 27, 28, 29)) 

¿私はこのエラーを取得する理由を誰もが知っていますか?

このコードは、ドラッグアンドドロップすると値を更新します。

ありがとうございました。

UPDATE

コントローラの機能は今ここに次のようになります。私はlaravel.logで見つけ

public function updateOrder(Request $request) 
    { 
     $ids = $request->ids; 
     // en el array ids, los "keys" serían el orden, los cuales puedes modificar según lo que requieras 

     $caseQuery = 'CASE'; 
     foreach ($ids as $order => $id) { 
      $caseQuery .= "WHEN id = $id THEN $order "; 
     } 
     $caseQuery .= 'ELSE null'; 
     $caseQuery .= ' END'; 
      DB::table('projects') 
       ->whereIn('id', $ids) 
       ->update(['order' => $caseQuery]); 

    } 

エラーはこれです:

Next Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'CASEWHEN id = 26 THEN 0 WHEN id = 23 THEN 1 WHEN id = 19 THEN 2 WHEN id = 25 THEN 3 WHEN id = 27 THEN 4 WHEN id = 18 THEN 5 WHEN' for column 'order' at row 1 (SQL: update projects set order = CASEWHEN id = 26 THEN 0 WHEN id = 23 THEN 1 WHEN id = 19 THEN 2 WHEN id = 25 THEN 3 WHEN id = 27 THEN 4 WHEN id = 18 THEN 5 WHEN id = 28 THEN 6 WHEN id = 14 THEN 7 WHEN id = 24 THEN 8 WHEN id = 20 THEN 9 WHEN id = 12 THEN 10 WHEN id = 13 THEN 11 WHEN id = 17 THEN 12 WHEN id = 29 THEN 13 WHEN id = 15 THEN 14 WHEN id = 21 THEN 15 WHEN id = 16 THEN 16 WHEN id = 22 THEN 17 WHEN id = 4 THEN 18 WHEN id = 3 THEN 19 WHEN id = 1 THEN 20 WHEN id = 6 THEN 21 WHEN id = 9 THEN 22 WHEN id = 8 THEN 23 WHEN id = 5 THEN 24 WHEN id = 2 THEN 25 WHEN id = 10 THEN 26 WHEN id = 11 THEN 27 WHEN id = 7 THEN 28 ELSE null END where id in (26, 23, 19, 25, 27, 18, 28, 14, 24, 20, 12, 13, 17, 29, 15, 21, 16, 22, 4, 3, 1, 6, 9, 8, 5, 2, 10, 11, 7)) in /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Database/Connection.php:647 Stack trace: #0 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Database/Connection.php(607): Illuminate\Database\Connection->runQueryCallback('update project...', Array, Object(Closure)) #1 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Database/Connection.php(477): Illuminate\Database\Connection->run('update project...', Array, Object(Closure)) #2 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Database/Connection.php(416): Illuminate\Database\Connection->affectingStatement('update project...', Array) #3 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2152): Illuminate\Database\Connection->update('update project...', Array) #4 /home/vagrant/Code/Manifiesto/app/Http/Controllers/AdminController.php(141): Illuminate\Database\Query\Builder->update(Array) #5 [internal function]: App\Http\Controllers\AdminController->updateOrder(Object(Illuminate\Http\Request)) #6 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(55): call_user_func_array(Array, Array) #7 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(44): Illuminate\Routing\Controller->callAction('updateOrder', Array) #8 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Route.php(203): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\AdminController), 'updateOrder') #9 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Route.php(160): Illuminate\Routing\Route->runController() #10 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Router.php(574): Illuminate\Routing\Route->run() #11 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Routing\Router->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #12 /home/vagrant/Code/Manifiesto/vendor/vsch/laravel-translation-manager/src/RouteAfterMiddleware.php(21): Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #13 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Vsch\TranslationManager\RouteAfterMiddleware->handle(Object(Illuminate\Http\Request), Object(Closure)) #14 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #15 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #16 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure)) #17 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #18 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(43): Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #19 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Auth\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure), 'admin') #20 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #21 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(65): Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #22 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure)) #23 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #24 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #25 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure)) #26 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #27 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(64): Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #28 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure)) #29 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #30 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #31 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure)) #32 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #33 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #34 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure)) #35 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #36 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #37 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Router.php(576): Illuminate\Pipeline\Pipeline->then(Object(Closure)) #38 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Router.php(535): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request)) #39 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Router.php(513): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request)) #40 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request)) #41 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http{closure}(Object(Illuminate\Http\Request)) #42 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #43 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure)) #44 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #45 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #46 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure)) #47 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #48 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #49 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure)) #50 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #51 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #52 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure)) #53 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #54 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #55 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure)) #56 /home/vagrant/Code/Manifiesto/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request)) #57 /home/vagrant/Code/Manifiesto/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request)) #58 {main}

+0

私が期待していた生のクエリを確認してください。 –

+0

デバッグを行うにはどうしたらいいですか?私はそれを試してみる - > toSql()しかし、私はそれを行うことはできません私はクエリ()のget()が原因ではないと思う。 –

+0

あなたは 'CASE'の直前で注文の値を紛失しています。 –

答えて

0

はこれを試してみて、あなたのログを確認あなたのアプリの中で。

私はWHEN clausidを追加しました(ただし、適切にそのいくつかの他の変数の変化、それは場合)

UPDATE `projects` SET `order` = 
    CASE 
      WHEN id = 2 THEN 0 
      WHEN id = 3 THEN 1 
      WHEN id = 1 THEN 2 
      WHEN id = 4 THEN 3 
      WHEN id = 5 THEN 4 
      WHEN id = 6 THEN 5 
      WHEN id = 7 THEN 6 
      WHEN id = 8 THEN 7 
      WHEN id = 9 THEN 8 
      WHEN id = 10 THEN 9 
      WHEN id = 11 THEN 10 
      WHEN id = 12 THEN 11 
      WHEN id = 13 THEN 12 
      WHEN id = 14 THEN 13 
      WHEN id = 15 THEN 14 
      WHEN id = 16 THEN 15 
      WHEN id = 17 THEN 16 
      WHEN id = 18 THEN 17 
      WHEN id = 19 THEN 18 
      WHEN id = 20 THEN 19 
      WHEN id = 21 THEN 20 
      WHEN id = 22 THEN 21 
      WHEN id = 23 THEN 22 
      WHEN id = 24 THEN 23 
      WHEN id = 25 THEN 24 
      WHEN id = 26 THEN 25 
      WHEN id = 27 THEN 26 
      WHEN id = 28 THEN 27 
      WHEN id = 29 THEN 28 
     ELSE ? 
     END 
    WHERE id in (2, 3, 1, 4, 5, 6,7, 8, 
       9, 10, 11, 12, 13, 14, 
       15, 16, 17, 18, 19,20, 
       21, 22, 23, 24, 25, 26, 
       27, 28, 29) 

はSOこのために、この

$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]); 

変更:

$caseQuery = 'CASE'; 
     foreach ($ids as $order => $id) { 
      $caseQuery .= "WHEN id = $id THEN $order "; 
     } 
     $caseQuery .= 'ELSE null'; 
     $caseQuery .= ' END'; 
      DB::table('projects') 
       ->whereIn('id', $ids) 
       ->update(['order' => $caseQuery]); 
+0

¿?あなたのコードに関するいくつかの情報を書いてください。 –

+0

これは私のコードのクエリですか? –

+0

私はそれが動作し、ログにエラーはないと思います。それをlaravelクエリに渡すには? –

0

を空白のためにあなたのSQLを間違って構築しています。

このコード

$caseQuery = 'CASE'; 
foreach ($ids as $order => $id) { 
    $caseQuery .= "WHEN id = $id THEN $order "; 
} 

$caseQuery = 'CASE '; 
foreach ($ids as $order => $id) { 
    $caseQuery .= "WHEN id = $id THEN $order "; 
} 

に変更し、それをSQL文で

CASEWHEN id = 26 ... 

を結果であり、あなたが

CASE WHEN id = 26 ... 
を取得

注:クエリ全体に対してこれを行う必要があります。

+0

あなたのコードを探していて、それと同等に見えますか?あなたはそれを編集しないかもしれない。 –

+0

くそー私はちょうどエラーをチェックして、それは本当です。カケウハンのように見えます。どのラインを修正する必要がありますか? –

+0

@LluísPuigFerrer、私はエラーを指摘するまで停止した。私はすべてのあなたのコードを通過しませんでした。しかし、私はあなたに例を挙げました(あなたには 'CASE'の代わりに 'CASE'を使用してください。)あなたは自分で残りの部分を通過する必要があります。 – crafter

関連する問題