@tomasosho go to folder storage/logs and access the latest log file. scroll down to bottom and you will see the stack trace. The error must be defined there.
jquery-2.1.1.js:4 GET http://127.0.0.1:8000/productsCat?cat_id=2&price=500-1000 500 (Internal Server Error)
how can i solve this?
jquery-2.1.1.js:4 GET http://127.0.0.1:8000/productsCat?cat_id=2&price=500-1000 500 (Internal Server Error)
last error i got
[2020-10-30 11:50:54] local.ERROR: count(): Parameter must be an array or an object that implements Countable {"userId":5,"exception":"[object] (ErrorException(code: 0): count(): Parameter must be an array or an object that implements Countable at /Applications/XAMPP/xamppfiles/htdocs/afiaprime/app/Http/Controllers/SelectedCategoryController.php:69)
[stacktrace]
My controller
public function productsCat(Request $request){
$cat_id = $request->cat_id;
$priceCount = count($request->price);
// price are array
if($cat_id!="" && $priceCount!="0"){
$price = explode("-",$request->price);
$start = $price[0];
$end = $price[1];
//echo "both are selected";
$data = DB::table('products')
->join('product_categories','product_categories.id','products.categories_id')
->where('products.categories_id',$cat_id)
->where('products.price', ">=", $start)
->where('products.price', "<=", $end)
->get();
}
else if($priceCount!="0"){
$price = explode("-",$request->price);
$start = $price[0];
$end = $price[1];
//echo "price is selected";
$data = DB::table('products')
->join('product_categories','product_categories.id','products.categories_id')
->where('products.price', ">=", $start)
->where('products.price', "<=", $end)
->get();
}
else if($cat_id!=""){
//echo "cat is selected";
$data = DB::table('products')
->join('product_categories','product_categories.id','products.categories_id')
->where('products.categories_id',$cat_id)
->get();
}
else{
//echo "nothing is slected";
return "<h1 align='center'>Please select atleast one filter from dropdown</h1>";
}
if(count($data)=="0"){
echo "<h1 align='center'>no products found under this Category</h1>";
}else{
return view('frontEnd.produt-category',[
'data' => $data, 'catByUser' => $data[0]->cat_name
]);
}
}
You can ensure that they dont fail (null isnt countable)
$priceCount = count($request->price ?? []);
//and
if(count($data ?? [])=="0"){
[2020-10-30 12:12:13] local.ERROR: count(): Parameter must be an array or an object that implements Countable {"userId":5,"exception":"[object] (ErrorException(code: 0): count(): Parameter must be an array or an object that implements Countable at /Applications/XAMPP/xamppfiles/htdocs/afiaprime/app/Http/Controllers/SelectedCategoryController.php:69)
[stacktrace]
Weird, that should work
Try this instead
if(!$data) {
Correct?
public function productsCat(Request $request){
if(!$data) {
$cat_id = $request->cat_id;
$priceCount = count($request->price);
// price are array
if($cat_id!="" && $priceCount!="0"){
$price = explode("-",$request->price);
$start = $price[0];
$end = $price[1];
//echo "both are selected";
$data = DB::table('products')
->join('product_categories','product_categories.id','products.categories_id')
->where('products.categories_id',$cat_id)
->where('products.price', ">=", $start)
->where('products.price', "<=", $end)
->get();
}
else if($priceCount!="0"){
$price = explode("-",$request->price);
$start = $price[0];
$end = $price[1];
//echo "price is selected";
$data = DB::table('products')
->join('product_categories','product_categories.id','products.categories_id')
->where('products.price', ">=", $start)
->where('products.price', "<=", $end)
->get();
}
else if($cat_id!=""){
//echo "cat is selected";
$data = DB::table('products')
->join('product_categories','product_categories.id','products.categories_id')
->where('products.categories_id',$cat_id)
->get();
}
else{
//echo "nothing is slected";
return "<h1 align='center'>Please select atleast one filter from dropdown</h1>";
}
if(count($data)=="0"){
echo "<h1 align='center'>no products found under this Category</h1>";
}else{
return view('frontEnd.produt-category',[
'data' => $data, 'catByUser' => $data[0]->cat_name
]);
}
}
}
@tomasosho Try to understand the logic here. You have 4 blocks here. if-elseif-elseif-else. 1st three blocks has $data variable. But if the else {...} block is executed, then the $data variable will never be initiated. And thus, count($data) will fail, because it does not know what $data is.
So either change the bottom code to the following as @sinnbeck suggested.
if(isset($data) && count($data)=="0"){
echo "<h1 align='center'>no products found under this Category</h1>";
}else{
return view('frontEnd.produt-category',[
'data' => $data, 'catByUser' => $data[0]->cat_name
]);
Or at the top just define $data variable and keep everything same.
$data = [];
//...
As count 0 is false and null is false, this should be
if(!$data){
echo "<h1 align='center'>no products found under this Category</h1>";
}else{
return view('frontEnd.produt-category',[
'data' => $data, 'catByUser' => $data[0]->cat_name
]);
@gitwithravish Nice catch! ;)
Can you see in the stack trace where the error comes from?
And you can try fixing the product count like this (if that is the issue)
$priceCount = count($request->input('price', []);
@tomasosho is $request->price an array or its a simple string???
[2020-10-30 13:10:03] local.ERROR: count(): Parameter must be an array or an object that implements Countable {"userId":5,"exception":"[object] (ErrorException(code: 0): count(): Parameter must be an array or an object that implements Countable at /Applications/XAMPP/xamppfiles/htdocs/afiaprime/app/Http/Controllers/SelectedCategoryController.php:71)
[stacktrace]
Is there another way i can filter products from min to max, and also search through using jquery?
How about pasting you current version, and I will have a look for errors in it again.
I had to rewrite my blade file, I want to sort listed products in the category selected, i need help with the controller querying.
My Blade File
@extends('layouts.cat5')
@section('content')
@include('frontEnd.ourJs')
<div class="contact-container" style="align-items: flex-start;">
<div class="form-container lcontact-form" style="width: 100%;">
<h3 style="margin-bottom: revert; margin-top:12px;">Filter Category</h3>
<div style="display: inline-block; margin-top:12px; text-align: center;">
<input type="text" id="catID" value="{{$catByUser}}" hidden/>
<input type="number" id="minpriceID" min="0" class="form-control" style="margin: inherit;" placeholder="Min Price">
<input type="number" id="maxpriceID" min="0" class="form-control" style="margin: inherit;" placeholder="Max Price">
<input type="text" id="search" class="form-control" style="margin: inherit;" placeholder="search product by name">
<div style="margin-top:inherit;">
<button class="login-btn form-control" id="findBtn" style="margin: inherit; border-left: inset;"> <span class="fas fa-filter"></span> Filter</button>
</div>
</div>
</div>
<div class="gender-clothing">
<div class="title-row">
<div class="main-title">
<h3 style="font-size: xx-large;">{{$catname->name}} Category</h3>
</div>
</div>
<div class="cloth-row">
@if(count($data)=="0")
<h1 align="center" style="margin-top:20px">
No products found under
<b style="color:red">{{$catByUser}} </b>
Category
</h1>
@elseif (count($data)!="0")
@foreach($data as $p)
<div class="cloth" id="productData">
<div class="card">
<img src="/products/large/{{$p->image}}" alt="{{$p->image}}" style="width:100%">
<h1 style="margin-top: revert;">{{$p->p_name}}</h1>
<br>
<p class="price" style="color:#0d6cb3;">NGN {{$p->price}}</p>
<br>
<p><a href="/product-detail/{{$p->id}}/{{$p->slug}}"><button>Explore</button></a></p>
</div>
</div>
@endforeach
@endif
</div>
</div>
</div>
<!-- <div class="container">
<div class="row">
<div class="col-sm-3">
</div>
<div class="col-sm-9 padding-right">
@if(count($data)!="0")
<h1 class="text-center">{{$catByUser}} </h1>
<div class="row">
<div class="">
<select id="catID">
<option value="{{$catByUser}}">{{$catname->name}}</option>
</select>
</div>
<div class="">
<select id="priceID">
<option value="">Select Price Range</option>
<option value="100">0-100</option>
<option value="100-300">100-300</option>
<option value="300-500">300-500</option>
<option value="500-1000">500-1000</option>
</select>
<input type="number" id="priceID">
</div>
<div class="col-sm-6 hidden-xs">
<div class="row">
<div class="col-sm-4 pull-right">
<button id="findBtn" class="btn btn-success">Find</button>
</div>
<div class="styleNm">16 style(s)</div>
</div>
</div>
@endif
</div>
<div class="row top25">
@if(count($data)=="0")
<div class="col-md-12" align="center">
<h1 align="center" style="margin:20px">
No products found under
<b style="color:red">{{$catByUser}} </b>
Category </h1>
</div>
@else
<div id="productData">
@foreach($data as $p)
<div class="col-xs-6 col-sm-4" >
<div class="itemBox">
<div class="prod">
<img
src="/products/large/{{$p->image}}" alt="/products/large/{{$p->image}}"
width="400px" height="360px" /></div>
<label>{{$p->p_name}}</label>
<span class="hidden-xs">Code: {{$p->p_code}}
<br>
{{$p->pro_info}}</span>
<div class="addcart">
<div class="price">NGN {{$p->price}}</div>
<div class="cartIco hidden-xs"><a href="{{url('/cart/add')}}/{{$p->id}}"></a></div>
</div>
</div>
</div>
@endforeach
</div>
@endif
</div>
</div>
</div> -->
@endsection
ourJs
<script>
$(document).ready(function(){
$("#findBtn").click(function(){
var cat = $("#catID").val();
var minprice = $('#minpriceID').val();
var maxprice = $('#maxpriceID').val();
var search = $('#search').val();
$.ajax({
type: 'get',
dataType: 'html',
url: '{{url('/productsCat')}}',
data: 'cat_id=' + cat + 'min_price=' + minprice + 'max_price' + maxprice + 'search' + search,
success:function(response){
console.log(response);
$("#productData").html(response);
}
});
});
});
</script>
Web Route
Route::get('/selected-category/{id}/{slug}', 'SelectedCategoryController@show')->middleware('auth');
Route::get('selected-category/{id}/{slug}','SelectedCategoryController@show');
Route::get('productsCat','SelectedCategoryController@productsCat');
Controller
public function show($id, $slug, Request $request)
{
$cat = $id;
// dd($cat);
$catByUser = $cat;
$catname = ProductCategory::find($slug);
$subcategory = SubCategory::with(['productCategory'])->orderBy('name')->get();
$data = Product::where('categories_id', $cat)->get();
// dd($catname);
$cart = User::findorfail(auth()->user()->id)->cart;
$user = auth()->user()->id;
$messages = User::findorfail($user)->latestmessaging->count();
// dd($category);
return view('frontEnd.product-category', compact('data', 'catname', 'catByUser', 'subcategory', 'cart', 'messages'));
}
public function productsCat(Request $request){
$cat_id = $request->cat_id;
// dd(count($request->price));
$pricemax= $request->max_price;
$pricemin= $request->min_price;
$search= $request->search;
}
I need help with the querying...
i changed my blade files to
<div class="cloth-row" id="productData">
@if(count($data)=="0")
<h1 align="center" style="margin-top:20px">
No products found under
<b style="color:red">{{$catByUser}} </b>
Category
</h1>
@elseif (count($data)!="0")
@foreach($data as $p)
<div class="cloth">
<div class="card">
<img src="/products/large/{{$p->image}}" alt="{{$p->image}}" style="width:100%">
<h1 style="margin-top: revert;">{{$p->p_name}}</h1>
<br>
<p class="price" style="color:#0d6cb3;">NGN {{$p->price}}</p>
<br>
<p><a href="/product-detail/{{$p->id}}/{{$p->slug}}"><button>Explore</button></a></p>
</div>
</div>
@endforeach
@endif
</div>
<script>
$(document).ready(function(){
$("#findBtn").click(function(){
var cat = $("#catID").val();
var minprice = $('#minpriceID').val();
var maxprice = $('#maxpriceID').val();
var search = $('#search').val();
$.ajax({
type: 'get',
dataType: 'html',
url: '{{url('/productsCat')}}',
data: 'cat_id=' + cat, 'min_price=' + minprice, 'max_price=' + maxprice, 'search=' + search,
success:function(response){
console.log(response);
$("#productData").html(response);
}
});
});
});
</script>
i am getting null in my controller requests
$cat_id = $request->cat_id;
$pricemax= $request->max_price;
$pricemin= $request->min_price;
$search= $request->search;
dd($cat_id);
Please or to participate in this conversation.