2016-12-09 2 views
1

Laravel에 새로 왔습니다. 내가 만들 때 이죠 모든 것을 가진 컨트롤러에 대한 패치 요청은 OK입니다,하지만 난 jQuery를 아약스와 함께 수행하려고하지 않을 경우, 에러가 발생합니다 :패치 요청이있는 HttpNotFoundException laravel

HttpNotFoundException at vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php#161

내 경로 :

Route::group(['prefix' => 'home'], function() { 
    Route::patch('products/{id}','[email protected]')->middleware('auth'); 
}); 

내 컨트롤러 :

public function update(Request $request, $id) 
{ 
    $validator = Validator::make($request->all(), [ 
     'name' => 'required|max:60', 
     'id_category' => 'required|not_in:select', 
     'quantity' => 'required|numeric', 
     'purchase_price' => 'required|numeric', 
     'sale_price' => 'required|numeric', 
     'warning_level' => 'required|numeric', 
     'alert_level' => 'required|numeric', 
     'fpoints_togive' => 'numeric', 
     'fpoints_cost' => 'numeric', 
     'iva' => 'in:4,10,21' 
    ]); 
    if ($validator->fails()) 
     return Response::json (array (
      'errors' => $validator->getMessageBag()->toArray() 
     )); 
    else { 
     try { 
      DB::beginTransaction(); 

      //$id_shop = Shop_users::where('id_user', Auth::user()['attributes']['id']); 
      $id_shop = DB::select('select id_shop from shop_users where id_user = '.Auth::user()['attributes']['id'])[0]->id_shop; 

      $shop_pr = DB::table('shop_products') 
       ->where('id_shop', $id_shop) 
       ->where('id_product', $id) 
       ->select('quantity', 'purchase_price', 'sale_price', 'active', 'warning_level', 'alert_level', 'fpoints_togive', 'fpoints_cost'); 

      if($request->active == 'on') $active = 1; else $active = 0; 

      DB::table('shop_products') 
       ->where('id_product', $id) 
       ->update(
        ['quantity' => $request->quantity], 
        ['purchase_price' => $request->purchase_price], 
        ['sale_price' => $request->sale_price], 
        ['active' => $active], 
        ['warning_level' => $request->warning_level], 
        ['alert_level' => $request->alert_level], 
        ['fpoints_togive' => $request->fpoints_togive], 
        ['fpoints_cost' => $request->fpoints_cost] 
       ); 

      $shop_pr = DB::table('shop_products') 
       ->where('id_product', $id) 
       ->get(); 
      //dd($shop_pr); 

      DB::table('products') 
       ->where('id', $id) 
       ->update(
        ['name' => $request->name], 
        ['id_category' => $request->id_category], 
        ['iva' => $request->iva] 
       ); 

      $product = DB::table('products') 
       ->where('id', $id) 
       ->get(); 

      DB::commit(); 

      $category = DB::select('select name from categories where id = '.$request->id_category)[0]->name; 

      return response()->json ([ 
       'product' => $product, 
       'shop_pr' => $shop_pr, 
       'category' => $category 
      ]); 

     } catch(Exception $e) { 
      DB::rollBack(); 
      return Response::json (array (
       'errors' => array(
        'name' => 'Server error' 
       ) 
      )); 
     } 

    } 
} 

그리고 내보기 :

$('#updbtn').click(function(){ 
    $.ajax({ 
     type: 'patch', 
     url: '/home/products'+$('input[name=updid]').val(), 
     headers: { 
      'Content-Type':'application/x-www-form-urlencoded', 
     }, 
     data: { 
      '_token': '{!! csrf_token() !!}', 
      'quantity': $('input[name=updquantity]').val(), 
      'purchase_price': $('input[name=updprecio_compra]').val(), 
      'sale_price': $('input[name=updprecio_venta]').val(), 
      'active': $('input[name=updactive]').val(), 
      'warning_level': $('input[name=updnivel_aviso]').val(), 
      'alert_level': $('input[name=updnivel_alerta]').val(), 
      'fpoints_togive': $('input[name=updpuntos_entregados]').val(), 
      'fpoints_cost': $('input[name=updcoste_fidelidad]').val(), 
      'name': $('input[name=updp_name]').val(), 
      'iva': $('select[name=updiva] option:selected').val(), 
      'id_category': $('select[name=updcategory] option:selected').val() 
     }, 
     success: function(data) { 
      if ((data.errors)) { 
       $.notify({ 
        icon: 'fa fa-exclamation-triangle', 
        message: data.errors.name 
       },{ 
        type: 'danger' 
       }); 
      } else { 
       $.notify({ 
        icon: 'fa fa-check', 
        message: 'Producto modificado correctamente' 
       },{ 
        type: 'success' 
       }); 
       //var faactive; 
       //if(data.shop_pr.active == 1) {faactive = '<i class="fa fa-check"></i>'; } else { faactive = '<i class="fa fa-times"></i>' }; 
       //$('#dataTable tr:last').after('<tr id="cat'+data.product.id+'"><td>'+data.product.id+'</td><td>'+data.product.name+'</td><td>'+data.product.category+'</td><td>'+data.shop_pr.quantity+'</td><td>'+data.shop_pr.purchase_price+'€</td><td>'+data.product.iva+'%</td><td>'+data.shop_pr.sale_price+'€</td><td>'+data.shop_pr.warning_level+'</td><td>'+data.shop_pr.alert_level+'</td><td>'+data.shop_pr.fpoints_togive+'</td><td>'+data.shop_pr.fpoints_cost+'</td><td>'+data.shop_pr.updated_at+'</td><td>'+faactive+'</td><td><a data-toggle="modal" data-original-title="Modificar" href="#modifyModal" onclick="fillupdModal(&#39;'+data.id+'&#39;, &#39;'+data.name+'&#39;, &#39;'+data.updated_at+'&#39;)" class="btn btn-warning">Modificar</a></td><td><a data-toggle="modal" data-original-title="Eliminar" href="#deleteModal" onclick="filldelModal(&#39;'+data.id+'&#39;, &#39;'+data.name+'&#39;)" class="btn btn-danger">Eliminar</a></td></tr>'); 
      } 

     }, 
     error: function(data) { 
      $.notify({ 
       icon: 'fa fa-times', 
       message: 'Error interno del servidor, por favor inténtelo de nuevo más tarde' 
      },{ 
       type: 'danger' 
      }); 
     }, 
    }); 
}); 

많은 것을 시도했지만이 오류를 해결하는 방법을 모르겠습니다.

+0

나는 '집/제품'에 가려고 노력하고 있지만 귀하의 경로는 단지 '제품'이라고 말합니다. – tam5

+0

URL이 정확합니다. 나는/home 접두사가 있다는 것을 잊어 버렸습니다. –

+0

내가 생각할 수 있습니다 :) .. laravel가 제공하는 전체 오류 스택을 게시 할 수 있습니다 – tam5

답변

1

브라우저의 콘솔에서 네트워크 탭을 확인하는 경우에는 $.ajax 잘못된 url에 당신이 추가하는 때문에 /home에있는 요청을 보낸 것을 볼 수 있습니다 :

url: '/home/products'+$('input[name=updid]').val(), 

그것은해야한다 그냥 :

url: '/products'+$('input[name=updid]').val(), 

희망이 있습니다.

+0

URL이 정확합니다./home 접두어가 있다는 것을 잊어 버렸습니다. –

+0

탐색 콘솔에서 네트워크 탭을 확인 했습니까 ?? –

+0

나는 아약스 URL이 잘못되었다. 그러나 이제 해결되었습니다. –

관련 문제