爲什麼這個代碼給出未定義的變量:我在上面定義的變量爲$category_id;
$ CATEGORY_IDPHP - 未定義的變量,雖然該變量是全局定義
// 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。爲什麼錯誤仍然存在?
這是否與$category->$category_id
傾斜代碼有關?
是... $ category_id基本上是空的。沒有值。 – Daniel
$ category - > $ category_id;有一個值,但錯誤在「$ category_id =」s $ category_id。這是爲什麼? –
在哪一行上面出現錯誤!!,代碼 – sanjeev