Why does this code give a Undefined variable : $category_id
// declare a variable and an array
$category_id;
$posts = [];
// get four categories
$categories = Category::take(4)->get();
foreach($categories as $category){
//foreach of the four categories, get the category id
$category_id = $category->$category_id;
//add three posts of each of those categories to the $posts array
$posts[] = Category::where('category_id', $category_id)->take(3)->get();
}
Althought I defined the variable on the top as $category_id;. why does the error persist?
Is it something to do with the $category->$category_id skewing the code?