I hope you guys could understand what I need as result of the below codes.
$url = Route::getFacadeRoot()->current()->uri();
$sectionID = Section::select('id', 'url')->where('url', $url)->first()->toArray();
$categoryID = Category::select('id', 'url')->where('url', $url)->first()->toArray();
$brandID = Brand::select('id', 'url')->where('url', $url)->first()->toArray();
if (array_key_exists("id", $sectionID)) {
$productDetails = Product::where(['status' => 1, 'sections_id' => $sectionID['id']])->first()->toArray();
$totalCountProduct = count($productDetails);
} else if (array_key_exists("id", $categoryID)) {
$productDetails = Product::where(['status' => 1, 'categories_id' => $categoryID['id']])->first()->toArray();
$totalCountProduct = count($productDetails);
} else if (array_key_exists("id", $brandID)) {
$productDetails = Product::where(['status' => 1, 'brands_id' => $brandID['id']])->first()->toArray();
$totalCountProduct = count($productDetails);
} else {
$totalCountProduct = 0;
}
CodePudding user response:
You can do something like this:
$url = Route::getFacadeRoot()->current()->uri();
$section = Section::select('id')->where('url', $url)->first();
if ($section) {
return Product::where(['status' => 1, 'sections_id' => $section->id])->count();
}
$category = Category::select('id')->where('url', $url)->first();
if ($category) {
return Product::where(['status' => 1, 'categories_id' => $category->id])->count();
}
$brand = Brand::select('id')->where('url', $url)->first();
if ($brand) {
return Product::where(['status' => 1, 'brands_id' => $brand->id])->count();
}
return 0;
CodePudding user response:
I found the below code which is working fine for me.
$url = Route::getFacadeRoot()->current()->uri();
$sectionID = Section::select('id', 'url')->where('url', $url)->get()->pluck('id')->toArray();
$categoryID = Category::select('id', 'url')->where('url', $url)->get()->pluck('id')->toArray();
$brandID = Brand::select('id', 'url')->where('url', $url)->get()->pluck('id')->toArray();
if (array_key_exists("0", $sectionID)) {
$productDetails = Product::where(['status' => 1, 'sections_id' => $sectionID['0']])->get()->toArray();
$totalCountProduct = count($productDetails);
} else if (array_key_exists("0", $categoryID)) {
$productDetails = Product::where(['status' => 1, 'categories_id' => $categoryID['0']])->get()->toArray();
$totalCountProduct = count($productDetails);
} else if (array_key_exists("0", $brandID)) {
$productDetails = Product::where(['status' => 1, 'brands_id' => $brandID['0']])->get()->toArray();
$totalCountProduct = count($productDetails);
} else {
$totalCountProduct = 0;
}
CodePudding user response:
First of all you didn't tell us on which step you're getting error?
Second thing I'm supposing you are getting perfect result on following queries:
$url = Route::getFacadeRoot()->current()->uri();
$sectionID = Section::select('id', 'url')->where('url', $url)->first()-toArray();
$categoryID = Category::select('id', 'url')->where('url', $url)->first()-toArray();
$brandID = Brand::select('id', 'url')->where('url', $url)->first()->toArray();
Product count query:
$totalCountProduct = Product::where('status', 1)
->when(!empty($sectionID['id']) && $sectionID['id'], function($q){
$q->where('sections_id', $sectionID['id']);
})
->when(!empty($categoryID['id']) && $categoryID['id'], function($q){
$q->where('categories_id', $categoryID['id']);
})
->when(!empty($brandID['id']) && $brandID['id'], function($q){
$q->where('brands_id', $brandID['id']);
})
->count();
By this way you can add multiple conditions in single
query. and count()
clause is performance optimize it'll directly instruct mysql
to get only count of products
.
In your get()
clause case, it will scan full table and multple model will be fetched. When the size of your table increased it'll get slower and slower.