Shawdow's avatar

Sending the Ajax Request to the controller data not pasing to the controller

adding the filters for the List of Products i am trying to check data passing from the ajax Request if i print the console shows me error GET 500 (Internal Server Error) . how to print the passed ajax data to the console can anyone share solution??

controller code

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);
 
                
           }
  

blade file


@foreach($dproduct1 as $dproducts1)
    <input  id="colorId" value="{{ $dproducts1->id }}" type="checkbox"> {{ $dproducts1->attributevalue_1 }} <br>
@endforeach 


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

           var color = [];
      

            if($(this).is(":checked")){
            
                color.push($(this).val());
 
                   }
              
 
              Finalcolor = color.toString();
             

        $.ajax({

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

         success:function(response){

           console.log(response);
           
        }

     });
             
    });

0 likes
24 replies
biishmar's avatar

@Shawdow

You are sending color data to controller from ajax but u r using it outside the request->ajax() if condition, ajax function wont run outside if condition...

Check the route and ajax url r they same..

U r returning view inside request->ajax() if condition, return the json response

Shawdow's avatar

@biishmar first if condition is for price filtering next else if condition is for colors filtering so i have included outside the if condition that is in else if sir

so how can i include both the ajax request in one if else condition any suggestions sir!!

the route and ajax url is same!!

biishmar's avatar

@Shawdow

if(request->ajax()) { //request->ajax() will check where the request is ajax or not
    // code here runs if its ajax call.
    //i dont see price filter here.
} else if(isset($request->color)){
    //here normal call like get and post thats not from ajax runs with the condition if color value exist
    $colors = $request->color;
    
    dd($colors);
}
Shawdow's avatar

@biishmar sir i think the logic that u have told i have written is same.(i have modified still same error)

the query that i have written for the variable $product is for price filters only!!

 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)){
               
               if($request->color){
                   $colors = $request->color;
               
                  dd($colors); 
               }
              
                
           }
}
biishmar's avatar

@Shawdow

try this logic bro..

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

@biishmar bro i don't know why my logic not working.because the logic that i have written is correct with the ajax request when i print in the console the problem is in the controller so can u modify my logic give me suggestion!!

biishmar's avatar

@Shawdow

I have states all the problem in your logic by commenting check this and if you want my coding logic i can comment the usage of every line.. the coding i sent before actually i modified from your coding..

public function showsubcategory($id,$name,Request $request){
    // I get it u r using single controller for normal request and ajax request, i am sure that u dont get anything in page load because your page load data codings r inside ajax request condition...
    
    if($request->ajax()){ // this if condition runs only when ajax request, called so normal call wont go inside this if condition
        
        $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);// u should return this response not view, because this runs only for ajax request, not for normal request
        return view ('displayproducts',compact('product','subcatdata')); // u called view here inside ajax call return, this is not appropirate if u wnat only data
        
    } else if(isset($request->color)){ // this else if condition only runs for normal request not ajax request
        
        if($request->color){ // I think u want to filter the color by ajax request so this should come inside if condition not here
            $colors = $request->color;
            
            dd($colors);
        }
        
        
    }
}
Shawdow's avatar

@biishmar ty bro for your explanation i have another else condition where i have fetched all the brands from the database so i have little confuse using your logic!!!

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'));

}

biishmar's avatar

@Shawdow

You know u dont have to use else for if condition if its filter, i think u r trying to do filter for brands, what u have to do is just add another if condition under the color one , and change what you request and put where for specific column.. like this

if ( ! empty($request->brand) ) {
        $product = $product->where('brand', $request->color);
    }

and i can see that u are using eloquent and db, use eloquent relationship for getting everything related to product...

Shawdow's avatar

@biishmar now i am adding the filters for colors but i don't have column name like color instead of that i have attributevalue_1 to attributevalue_10 so in the attributevalue_1 column i have color value.

what i am trying to do is take the attributevalue_1's id value compare in the controller ajax request and display it!! hope you understand bro!!!

biishmar's avatar

@Shawdow

By ur coding i understand u r making ecommerce website, i am working in ecommerce company and i developed that site called www.retailgenius.com and developing an another one as freelancer.. so i get it

As long as u know the column name u can use it in where condition. Anyway u aint going to save any other thing in attributeValue_1 where u saving color..

Shawdow's avatar

@biishmar that's good bro !! i have fetched distinct attributevalue_1 from the database!!

Shawdow's avatar

@biishmar bro now i have if and else condition in controller. i have tried with my logic is does not work!!

so when it comes to your logic how can i include last else condition can you describe me!!

biishmar's avatar

@Shawdow

From my logic if u see it thoroughly i never used else condition, if u want to add another filter just copy the previous one and change the request->another and change db column in name..

 if ( ! empty( $request->start) && ! empty( $request->end) ) {
    $start =  $request->start;
    $end  = $request->end;
    
    $product = $product->where('price','>=',$start)
                       ->where('price','<=',$end);
}

if ( ! empty($request->color) ) {
    $product = $product->where('color', $request->color);
}

//New One

if ( ! empty($request->newrequest) ) {
    $product = $product->where('columnname', $request->newrequest);
}

$product = $product->get();

if ( $request->ajax() ) {
    return response()->json($product); // Forgot to put return here on previous one
}

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

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

From above coding just add another filter from the request..

Shawdow's avatar

@biishmar bro than what to do for this else condition because in this(else condition) only all the attributes values and brands i have passed!!

how can modify else condition from your above solution because if this else condition is not included filters values cannot be fetched!! so any idea bro!!

public function showsubcategory($id,$name,Request $request){

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'));

      }

}
biishmar's avatar

@Shawdow

$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();

these things r common shouldnt come in if or else, inside if u have to filter these according to filter value and return json response, in else u should return view

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();
    
    if($request->ajax()) {
        // filter the above data by filter request and return json response like
        
        if($request->price) {
            //filter the product by price
            $product = $product->where('price', $request->price); // this collection where
        }
        
        return [
            "product" => $product,// if you filter some other variable send those things too
        ];
    }
    else {
        return \view ('subcategory',compact('product','subcatdata','attribute', 'brand','catid'));
    }
}

R u creating e-commerce website?

Shawdow's avatar

@biishmar yeah creating ecommerce website!!

bro where to pass the filters return value view

  response()->json($product);
                return view ('displayproducts',compact('product','subcatdata'));
biishmar's avatar

@Shawdow

You r using ajax so returning view is inappropriate, return the filtered product or any other variable and manipulate using javascript..

Product view, is it page wise or scroll wise?

biishmar's avatar

@Shawdow

  • that guy using a view partial for ajax request.
  • return view part ans proceed the video but change the if else part like i said.
Shawdow's avatar

@biishmar kk but passing filters return variable to view i did not get you bro!!

biishmar's avatar

@Shawdow

by the video, u did your category page in complex way, kinda confused now..

Shawdow's avatar

@biishmar yeah u are exactly right!! we are making the thread longer that's not good for laravelers to find the solution if they faced same problem like me.

biishmar's avatar

@Shawdow

this is my skype id vivek1841, i think if i see ur whole code i can make ur coding best, if u have doubt in e-commerce, can contact me

Please or to participate in this conversation.