Skip to content

Instantly share code, notes, and snippets.

@imam932
Created November 14, 2020 08:37
Show Gist options
  • Save imam932/1dcf58511cdf90bbf33f8232cbe705a5 to your computer and use it in GitHub Desktop.
Save imam932/1dcf58511cdf90bbf33f8232cbe705a5 to your computer and use it in GitHub Desktop.
Get data multiple category id with whereIn
<?php
public function index($slug)
{
$parent = Category::where('slug', $slug)->first();
$child = Category::where('parent_id',$parent->id)->get();
$category_id = $parent->id;
$child_array_id = [];
foreach ($child as $key => $value) {
$child_array_id[$key] = $value->id;
}
$count = count($child_array_id)+1;
$mix_cat_id = $child_array_id[$count] = $category_id;
$reviews = Review::whereIn('product_id', function($query) use ($child_array_id){
$query->select('id')
->from(with(new Product)->getTable())
->whereIn('category_id', $child_array_id);
})
->orderBy('created_at', 'desc')
->paginate(5);
return view('app.categories.index', compact('parent', 'child', 'reviews'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment