Error Ajax and Laravel 500 (Internal Sever Error)

Viewed 79

I'm attempting to use AJAX in Chrome, but it's not working. This is my code, which you can see below. This is a copy of my web.php file.

Route::view('menu','home.menu',['categories'=> App\Categories::orderBy('name')->get()->take(11),'active' => 'menu'])->name('menu');
Route::get('/products','MenuController@getproducts');

This is my ajax

$.ajax({
      type: "GET",
      dataType:'html',
      url: "{{url('/products')}}",
      data: "cat_id="+ id,
      success: function(response){
        console.log(response)
        $("#productData").html(response)
      },
      error: function(data) {
        console.log('Error:', data);
      },
    });

This is the code for my controller method.

    public static function getproducts(Request $request)
    {
        get productsd = $request->cat_id;
        $categories = Categories::orderBy('name')->get()->take(11);
        $collection = DB::table('products')
                      ->join('product_category', 'products.id','=', 'product_category.product_id')
                      ->where('product_category.category_id', '=', $category_id)
                      ->get();
        return view('home.menu',[
            'data' => $collection,
        ]);
    }
Facade\Ignition\Exceptions\ViewException: Undefined variable: categories (View: /Applications/XAMPP/xamppfiles/htdocs/shop/resources/views/home/menu.blade.php) in file /Applications/XAMPP/xamppfiles/htdocs/shop/resources/views/home/menu.blade.php on line 28
1 Answers

One change you can do is in Ajax for below line.

data: "cat_id="+ id,

So better to pass

data: {cat_id:id}

you are not using received category id from request in your query while returning view ,you are not passing categories

return view('home.menu',[
            'data' => $collection,
            'categories'=>$categories
        ]);
Related