I am trying to use AJAX to get sorted products off of a database. However AJAX keeps sending this error.
exception: "ErrorException" file: "C:\xampp\htdocs\WebProject\storage\framework\views\c39cbea2eafe7197b8efb68251988b2c5b3a6834.php" line: 5 message: "Trying to get property 'URL' of non-object (View: C:\xampp\htdocs\WebProjectesources\views\producto_indiv.blade.php)"
The thing is that the view producto_indiv has no connection to the javascript code for ajax. The view where the error comes from shows no errors when being accessed.
This is the producto_indiv, where the error is supposed to come from, however this view is unrelated to the one with the ajax code
<div class="contenedor" id="Producto_Indiv_Page">
<div class="item-cont">
<?php $url = "/storage/images/products/" . basename($producto->URL); ?>
<img src="{{$url}}">
<div class="line"></div>
<div class="traits">
<div><span class="name">{{$producto->nombre_prod}}</span></div>
<span>Precio: ${{$producto->precio}}</span>
<span>Descripcion: {{$producto->descripcion}}</span>
<span>marca: {{$producto->marca}}</span>
<span>tamanos: {{$producto->tamanos}}</span>
</div>
</div>
</div>
AJAX code
$('.sort_opt').click(function(e) {
e.preventDefault();
jQuery.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
jQuery.ajax({
dataType: 'json',
type: 'GET',
url: '/ropa/sort',
data: {
type: this.name,
genero: this.gender,
},
success: function(result) {
console.log("DONE");
},
error: function(result) {
console.log(result);
}
});
});
and controller
public function sort(Request $request) {
$producto = DB::table('productos')->where('$request->genero')->get();
switch($request->type) {
case 'nombre':
$producto->orderBy('nombre_prod', 'asc');
break;
case 'marca':
$producto->orderBy('marca', 'asc');
break;
case 'precio_desc':
$producto->orderBy('precio', 'desc');
break;
case 'precio_desc':
$producto->orderBy('precio', 'desc');
break;
case 'new':
$producto->latest();
break;
case 'old':
$producto->oldest();
break;
}
return \Response::json($producto);
}
I have been stuck on this before but changed to using redirects instead of ajax, however in this I cannot do that due to the fact that I am trying to sort a grid of products, sent from the DB.