Error Ajax and Laravel 500 (Internal Sever Error) Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Error Ajax and Laravel 500 (Internal Sever Error) without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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'=> AppCategories::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,
        ]);
    }
FacadeIgnitionExceptionsViewException: 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

Answer

One change you can do is in Ajax.data: "cat_id="+ id, 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
        ]);
We are here to answer your question about Error Ajax and Laravel 500 (Internal Sever Error) - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji