Laravel Ajax发布数据

Laravel Ajax发布数据

问题描述:

I have an eCommerce shop where I have to filter products by its categories.

I have to post an array of all filters in the controller, but I am getting this error

 "message": "",
    "exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
    "file": "C:\\xampp\\htdocs\\web_front\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\RouteCollection.php",
    "line": 179

Here is my code:

 <script>
    $(document).ready(function () {
        var categories = [];
        $('input[name="cat[]"]').on('change', function (e) {
            e.preventDefault();
            categories = []; // reset
            $('input[name="cat[]"]:checked').each(function()
            {
                categories.push($(this).val());
            });



            $.ajax({

                type:"GET",

                url:'advanced_filter/' + categories,


                success:function(data){

                    console.log(data);
                },
                error: function(xhr,errmsg,err)
                {
                    console.log(xhr.responseText);
                }
            });
        });
    });

</script>

web.php

Route::get('advanced_filter/{filters}', 'HomepageController@advanced_filter')->name('advanced_filter');


 public function advanced_filter($filters)
    {
dd($filters);
}

I am trying to show all the filters, so I can make a query to get all the products based on filters. I have created the script where I get all filters and want to post it in the controller. Please, can you see this? Thank you

                    

我有一家电子商务商店,必须在其中按类别过滤产品。 p>

我必须在控制器中发布所有过滤器的数组,但出现此错误 p>

 “ message”:“”,
     “ exception”:“ Symfony \\ Component \\ HttpKernel \\ Exception \\ NotFoundHttpException”,
     “ file”:“ C:\\ xampp \\ htdocs \\ web_front \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ RouteCollection.php”,
     “线”:179
  code>  pre>

 

这是我的代码: p>

  

first of all :

"exception":"Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",

above exeption occur when route is not found . NotFoundHttpException has status 404

in your code you are passing array of categories in route params , which is syntactically wrong , so thats why failed to find route .

Route::get('advanced_filter', 'HomepageController@advanced_filter')->name('advanced_filter');

from frontend side : pass categories as query parameters

url will be :

/advanced_filter?queryParameter= ids of selected categories

/advanced_filter?categories=1,3,7

your ajax function will be :

$.ajax({
     type:"GET",
     url:`/advanced_filter?categories=${categories.join(',')}`,
     success:function(data){
             console.log(data);
      },
      error: function(xhr,errmsg,err){
             console.log(xhr.responseText);
      }
   });

in your controller :

use Illuminate\Http\Request;

public function advanced_filter(Request $request)
{
    $filter_categories=[];
    if($request->has('categories')){
      $filter_categories=$request->query('categories');
    }

    /* you can pass extra query parameter like sortBy,sortOrder,limit  in url 
     `/advanced_filter?categories=1,3,7&limit=24&sortBy=name&sortOrder=desc`
 */

     $sortBy=$request->has('sortBy')?$request->query('sortBy'):'id';
     $sortOrder=$request->has('sortOrder')?$request->query('sortOrder'):'desc';
     $limit = $request->has('limit')?$request->has('limit'):12;

     /* assuming you have models with relations */

     $query = Products::query();
     $query->with('categories');
     if(count($filter_categories)>0){
        $query->whereHas('categories', function ($q) use ($filter_categories) {
            $q->whereIn('categories.id',$filter_categories);
        });
     }
     $query->orderBy($sortBy,$sortOrder);
     $products = $query->paginate($limit);
     return response()->json($products);

}