简体   繁体   中英

Laravel 5.6 using ajax

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\\WebProject\\resources\\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.

This problem has arrived because your $producto object has no Url, so please check first the $producto->URL and then pass the URL as below way.

<?php 
$productURl = "";
if(isset($producto->URL)) {
  $productURl = $producto->URL;
}

?>

<div class="contenedor" id="Producto_Indiv_Page">
  <div class="item-cont">
    <?php $url = "/storage/images/products/" . basename($productURl); ?>
    <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>

There are many things wrong here:

  1. Your route ropa/{id} overrides /ropa/sort because it matches when id="sort" so you need to either put the ropa/sort route before the ropa/{id} route.

  2. You are calling ->get() which executes the query and subsequently you are adding query constraints. That will not work since the query is already executed.

  3. You have where('$request->genero') that that does not actually use any variable because you've wrapped it in a string.
  4. where('$request->genero') does not have a column to filter by, the syntax would be where('genero', $request->genero)

Putting all these 4 together I can say :

Put the ropa/sort before the ropa/{id} route.

Change your sort() code to:

public function sort(Request $request) {
    $producto = DB::table('productos')->where('genero', $request->genero);
    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->get());

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM