Shawdow's avatar

Calling AJAX request to the controller

below the AJAX code written for Price and Color how to call both the AJAX request to the controller File

<script>
    $(function() {
     $( "#slider-range" ).slider({
      range: true,
      min: {{ $minprice }},
      max: {{ $maxprice }},
      values: [ {{ $minprice }}, {{ $maxprice }}],
      slide: function( event, ui ) {
       $( "#amount_start" ).val( "" + ui.values[ 0 ]);

       $( "#amount_end" ).val( "" + ui.values[ 1 ]);
 
     var start = $('#amount_start').val();

     var end = $('#amount_end').val();


         $.ajax({

             type: 'get',
             url:'showsubcategory',
             data:"start=" + start + "& +end=" +end,

         success:function(response){

            console.log(response);
            $('#updateDiv').html(response);
        }

        });
            }
      });
 });

    $(function() {
    
        $('#colorId').click(function(){ 

         
           var color = [];
                
        if($(this).is(":checked")){
            
                color.push($(this).val());
          
                   }
          
              Finalbrand = color.toString();
          
        $.ajax({

             type: 'get',
             url:'showsubcategory',
             data: "color=" + Finalbrand,

         success:function(response){

            console.log(response);
            $('#updateDiv').html(response);
        
           
        }
     });
             
    });

 });
</script>

controller file (here i need to pass the AJAX request!!)

public function showsubcategory($id,$name,Request $request){
              
    $subcategory = \App\Product::where('id',$id)->where('name',$name)->first();
        
        $subcatdata = DB::table('categories')->select('*')->get();
        
        $product = DB::table('products')->select('*')->where('category_id','=',$id)->get();
                
        $prod =  \App\Product::get();

        $subids = $prod->pluck('attribute_id');
             
        $attribute =  \App\Attribute::whereIn('id', $subids)->get();
                  
        $brand =  DB::table('brands')->select('*')->get();

    return \view ('subcategory',compact('product','subcatdata','attribute',
             'brand','catid'));

}

0 likes
4 replies
ahmeddabak's avatar

first you need to register a route, please use your controller class name

Route::get('showsubcategory','Controller_Name_Here@showsubcategory');

then in your controller change the method to

public function showsubcategory(Request $request){

and inside of it fetch the data using the $request object

$id = $request->get('id');
$name= $request->get('name');

more info can be found in the laravel docs https://laravel.com/docs/5.6/controllers#dependency-injection-and-controllers

Shawdow's avatar

@ahmeddabak i have used the route than defined the controller written the showsubcategory function in the contoller

public function showsubcategory(Request $request){

the problem calling the AJAX Request to the controller so i have called AJAX Request as below but the AJAX request passed only for if condition not for other if else so i need to call the AJAX request in if else condition

The problem calling the AJAX Request to the controller i have used if else condition.AJAx passes only for first if condition not for other if else condition.how to call the two or more AJAX request in the controller!!!

written AJAX request in controller

public function showsubcategory($id,$name,Request $request){
            
        if($request->ajax()){
            
            $start =  $request->start;
            $end  = $request->end;
            
            $product = DB::table('products')->select('*')
                     ->where('category_id','=',$id)
                     ->where('price','>=',$start)
                     ->where('price','<=',$end)
                     ->get();
            
            $subcatdata = DB::table('categories')->select('*')->get();
            
              
                 response()->json($product);
                return view ('displayproducts',compact('product','subcatdata'));
                
           } else if(isset($request->color)){
               
               $colors = $request->color;
               
               dd($colors);
 
                
           } else{

    $subcategory = \App\Product::where('id',$id)->where('name',$name)->first();
        
        $subcatdata = DB::table('categories')->select('*')->get();
        
        $product = DB::table('products')->select('*')->where('category_id','=',$id)->get();
                
        $prod =  \App\Product::get();

        $subids = $prod->pluck('attribute_id');
             
        $attribute =  \App\Attribute::whereIn('id', $subids)->get();
                  
        $brand =  DB::table('brands')->select('*')->get();

    return \view ('subcategory',compact('product','subcatdata','attribute',
             'brand','catid'));
}

ahmeddabak's avatar

yes!!, this how if and else if works if you want if to be executed and the another if inside of it just remove the else before if

             
    public function showsubcategory( Request $request ) {

                $start = $request->start;
        $end   = $request->end;
        $id    = $request->id;
        $name  = $request->name;

        if ( $request->ajax() ) {

            $product = DB::table( 'products' )->select( '*' )
                         ->where( 'category_id', '=', $id )
                         ->where( 'price', '>=', $start )
                         ->where( 'price', '<=', $end )
                         ->get();

            $subcatdata = DB::table( 'categories' )->select( '*' )->get();


            response()->json( $product );

            return view( 'displayproducts', compact( 'product', 'subcatdata' ) );

            if ( isset( $request->color ) ) {

                $colors = $request->color;

                dd( $colors );

            }

        } else {

            $subcategory = \App\Product::where( 'id', $id )->where( 'name', $name )->first();

            $subcatdata = DB::table( 'categories' )->select( '*' )->get();

            $product = DB::table( 'products' )->select( '*' )->where( 'category_id', '=', $id )->get();

            $prod = \App\Product::get();

            $subids = $prod->pluck( 'attribute_id' );

            $attribute = \App\Attribute::whereIn( 'id', $subids )->get();

            $brand = DB::table( 'brands' )->select( '*' )->get();

            return \view( 'subcategory', compact( 'product', 'subcatdata', 'attribute',
                'brand', 'catid' ) );
        }
    }
Shawdow's avatar

@ahmeddabak but including another return view inside the if does not work sir!!

public function showsubcategory( Request $request ) {

                $start = $request->start;
        $end   = $request->end;
        $id    = $request->id;
        $name  = $request->name;

        if ( $request->ajax() ) {

            $product = DB::table( 'products' )->select( '*' )
                         ->where( 'category_id', '=', $id )
                         ->where( 'price', '>=', $start )
                         ->where( 'price', '<=', $end )
                         ->get();

            $subcatdata = DB::table( 'categories' )->select( '*' )->get();


            response()->json( $product );

            return view( 'displayproducts', compact( 'product', 'subcatdata' ) );

            if ( isset( $request->color ) ) { //this if does works!!

                $colors = $request->color;
    
               $product = DB::table('products')->select('*')
                         ->where('category_id','=',$id)
                         ->where('id','=',$colors)
                          ->get();


          $subcatdata = DB::table( 'categories' )->select( '*' )->get();


            response()->json( $product );

            return view( 'displayproducts', compact( 'product', 'subcatdata' ) );
            }

        } else {

            $subcategory = \App\Product::where( 'id', $id )->where( 'name', $name )->first();

            $subcatdata = DB::table( 'categories' )->select( '*' )->get();

            $product = DB::table( 'products' )->select( '*' )->where( 'category_id', '=', $id )->get();

            $prod = \App\Product::get();

            $subids = $prod->pluck( 'attribute_id' );

            $attribute = \App\Attribute::whereIn( 'id', $subids )->get();

            $brand = DB::table( 'brands' )->select( '*' )->get();

            return \view( 'subcategory', compact( 'product', 'subcatdata', 'attribute',
                'brand', 'catid' ) );
        }
    }

Please or to participate in this conversation.