Hi there I´m new in this community. I´m developing a web application with Laravel 5.1, and I want to call the method store of my model Sale
from an ajax POST request, but I have this from console:
MethodNotAllowedHttpException in RouteCollection.php line 218
What could it be? thanks a lot.
these are my routes:
//Rutas para inicios de sesión
Route::resource('log', 'LogController');
Route::get('logout','LogController@logout');
//Rutas generales
Route::get('/', 'WelcomeController@index');
Route::get('index', 'WelcomeController@indexdos');
//Routes for users
Route::get('user/actualizar','UserController@actualizar');
Route::get('user/deleter','UserController@deleter');
Route::resource('user', 'UserController');
//Routes for pacients
Route::get('pacient/deleter','PacientController@deleter');
Route::get('pacient/actualizar','PacientController@actualizar');
Route::resource('pacient', 'PacientController');
//Routes for medicaments
Route::get('medicament/deleter','MedicamentController@deleter');
Route::get('medicament/actualizar','MedicamentController@actualizar');
Route::resource('medicament', 'MedicamentController');
//Routes for doctors
Route::get('doctor/deleter','DoctorController@deleter');
Route::get('doctor/actualizar','DoctorController@actualizar');
Route::resource('doctor', 'DoctorController');
//Routes for dates
Route::get('date/deleter','DateController@deleter');
Route::get('date/actualizar','DateController@actualizar');
Route::get('date/bitacora','DateController@bitacora');
Route::resource('date', 'DateController');
//Routes for sales
Route::resource('sale', 'SalesController');`
This is the form that contents the values I want to send to sale.store
@extends('layouts.principal')
@include('Ventas.forms.barra')
@section('content')
<div class="container">
{!!Form::open()!!}
<input type="hidden" name="_token" value="" id="token"></input>
@include('Ventas.forms.sale')
<button id="btnAgregar" data-bind="click: $root.agregarVenta" class="btn btn-success">Registrar venta</button>
{!!Form::close()!!}
</div>
@stop
As You can see, I include the 'sale' form, where I include knockout js code:
<div class="form-group">
<table>
<tr>
<td>{!!Form::label('id_doctor_1','Cita a la cual se facturará: ')!!}</td>
<td><select data-bind="options: $root.citas, optionsText: function(item) {
return item.paciente() + ' (Fecha: ' + item.fecha() + ' , Hora: ' +item.hora() + ')'
},
value: selectedDate,
optionsCaption: 'Elija una cita...'"></select></td>
</tr>
<tr>
<td>{!!Form::label('Producto:')!!}</td>
<td><select data-bind="options: $root.productos, optionsText: function(item) {
return item.nombre() + ' (Precio de venta: ' + item.precio_venta() + ' ,Existencias: ' +item.existencias() + ')'
},
value: selectedProduct,
optionsCaption: 'Elija un producto...'"> </select></td>
<tr>
<td>{!!Form::label('cantidad:')!!}</td>
<td><input type="number" data-bind="value: cantidad"></input></td>
</tr>
</table>
</div>
<div class="form-group">
<label for="btnAgregar" class="col-sm-2 control-label"></label>
<div class="col-sm-10">
<button id="btnAgregar" data-bind="click: $root.agregarProducto_venta" class="btn btn-primary col-sm-8 col-md-offset-1">Agregar producto a la venta actual</button>
</div>
</div>
<div class="form-group">
<br><br/><br><br/>
</div>
<div data-bind="visible: selectedDate"> <!-- Appears when you select something -->
<div class="post format-image box masonry-brick">
<!-- Begin Blog Grid -->
<div class="blog-wrap">
<table>
<tr>
<td><strong>Detalles de cita elegida</strong></td>
</tr>
<tr>
<td><strong>Paciente:</strong></td>
<td><span data-bind="text: selectedDate() ? selectedDate().paciente : 'unknown'"></span></td>
</tr>
<tr>
<td><strong>Producto:</strong></td>
<td><span data-bind="text: selectedDate() ? selectedDate().producto : 'unknown'"></span></td>
</tr>
<tr>
<td><strong>Cantidad:</strong></td>
<td><span data-bind="text: selectedDate() ? selectedDate().cantidad : 'unknown'"></span></td>
</tr>
<tr>
<td><strong>Subtotal:</strong></td>
<td><span data-bind="text: selectedDate() ? selectedDate().subtotal : 'unknown'"></span></td>
</tr>
<tr>
<td><br><br></td>
</tr>
<tr>
<td><strong>Productos a facturar</strong></td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
</table>
<table>
<thead>
<tr><th>Nombre del producto____|</th><th>Precio____|</th><th>Cantidad____|</th><th>Subtotal____|</th><th>Acción____|</th></tr>
</thead>
<tbody data-bind="foreach: productos_venta">
<tr>
<td data-bind="text: producto"></td>
<td data-bind="text: precio"></td>
<td data-bind="text: cantidad"></td>
<td data-bind="text: subtotal"></td>
<td><a href="#" data-bind="click: $root.removeProduct">Remover</a></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<h3 style="color:#FAFAFA" data-bind="visible: totalSurcharge() > 0">
Total (incluyendo medicamentos y cita): $<span data-bind="text: totalSurcharge().toFixed(2)"></span>
</h3>
And finally this is my app.js
file, It is supposed that when I click on the button btnAgregar
, the method agregarVenta
makes an ajax request type POST, but it doesn´t work, what am I doing wrong? Again, thanks a lot.
File app.js
(ajax method):
//Método para agregar una nueva venta al arreglo de ventas, y después pasarlo
//a la base de datos.
self.agregarVenta=function(){
// crea un nuevo objeto Venta y después lo añade al arreglo.
var id_cita = self.selectedDate().id();
var venta= new Venta({id_cita: id_cita,producto: self.selectedDate().producto(),
cantidad: self.selectedDate().cantidad(),subtotal: self.selectedDate().subtotal()});
self.ventas().push(venta);
for (var i = 0; i < self.productos_venta().length; i++) {
venta= new Venta({id_cita: id_cita,producto: self.productos_venta()[i].producto(),
cantidad: self.productos_venta()[i].cantidad(),subtotal: self.productos_venta()[i].subtotal()});
self.ventas().push(venta);
}
var token = $("#token").val();
var ruta = "sale.store"
$.ajax({
route: ruta,
headers: {'X-CSRF-TOKEN': token},
type: 'POST',
data: {ventas: self.ventas()},
dataType: 'JSON',
error: function(respuesta) {alert("error");},
success: function(respuesta) {
if (respuesta) {
alert("exito");
}else {
alert("error");
}
}
});
}
}
via Chebli Mohamed