私はLaravelを新しくしました。私はポストマンとコントローラへのパッチ要求を行うと、すべてがOKですが、私はjQueryのAJAXをどうしようとすると、エラーがスローされない: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(''+data.id+'', ''+data.name+'', ''+data.updated_at+'')" class="btn btn-warning">Modificar</a></td><td><a data-toggle="modal" data-original-title="Eliminar" href="#deleteModal" onclick="filldelModal(''+data.id+'', ''+data.name+'')" 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'
});
},
});
});
私は多くのことを試しましたが、このエラーを解決する方法はわかりません。
私はあなたのjsです。あなたは「home/products」に行きますが、あなたのルートは「products」と書かれています。 – tam5
URLは正しいです、私は/ homeプレフィックスを持っていると忘れています。 –
私はそれがあるかもしれないと思った:) .. laravelが与える完全なエラースタックを投稿することができます – tam5