属性 [lesson_id] 在此集合实例上不存在 - 尽管我使用的是 foreach,因此仅检索 1 个模型
Property [lesson_id] does not exist on this collection instance - EVEN THOUGH I'm using a foreach and therefore retrieving only 1 model
我有一个简单的 LessonUser table 和模型 - 3 列 + 时间戳。
我正在使用 Laravel 8 并进行 TDD。
我从 table:
中获取了一些数据
/** get the latest lessonUser for each lesson_id */
$latestUserLessons =
LessonUser::
select('user_id', 'lesson_id', 'status_id')
->where('user_id', $user)
->latest()
->get()
->groupBy('lesson_id')
;
如果我运行这个:
foreach ($latestUserLessons as $latestUserLesson)
{
dd($latestUserLesson);
}
我得到:
Illuminate\Database\Eloquent\Collection {#2731
#items: array:1 [
0 => App\Models\LessonUser {#2741
#guarded: []
#connection: "mysql"
#table: "lesson_users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:3 [
"user_id" => 1
"lesson_id" => 1
"status_id" => 1
]
#original: array:3 [
"user_id" => 1
"lesson_id" => 1
"status_id" => 1
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
]
}
所以我明明有 'lesson_id' 属性.
但是当我运行这个:
foreach ($latestUserLessons as $latestUserLesson)
{
dd($latestUserLesson->lesson_id);
}
我感到害怕:
Property [lesson_id] does not exist on this collection instance.
at C:\xampp\htdocs\tdd\vendor\laravel\framework\src\Illuminate\Collections\Traits\EnumeratesValues.php:850
846▕ */
847▕ public function __get($key)
848▕ {
849▕ if (! in_array($key, static::$proxies)) {
➜ 850▕ throw new Exception("Property [{$key}] does not exist on this collection instance.");
851▕ }
852▕
853▕ return new HigherOrderCollectionProxy($this, $key);
854▕ }
1 C:\xampp\htdocs\tdd\app\Http\Controllers\SeshController.php:30
Illuminate\Support\Collection::__get("lesson_id")
2 C:\xampp\htdocs\tdd\vendor\laravel\framework\src\Illuminate\Routing\Controller.php:54
App\Http\Controllers\SeshController::buildSesh()
Tests: 1 failed
Time: 1.17s
我知道如果我尝试从集合中获取 属性 会出现此错误,但这里我显然有一个模型实例。
我试过:使用 eloquent 进行数据检索;使用带有增量的 'for' 循环,直到检索到的模型计数,控制数组位移等,我只是不断收到相同的错误。
我错过了什么吗?是否有另一种获取该字段数据的方法?
啊!!求助!!
您有 collection 个 collection。您必须迭代嵌套在其中的 collection 和 collection 以获得 LessonUser 模型。
foreach ($latestUserLessons as $group) {
foreach ($group as $latestUserLesson) {
dd($latestUserLesson->lesson_id);
}
}
我有一个简单的 LessonUser table 和模型 - 3 列 + 时间戳。
我正在使用 Laravel 8 并进行 TDD。
我从 table:
中获取了一些数据/** get the latest lessonUser for each lesson_id */
$latestUserLessons =
LessonUser::
select('user_id', 'lesson_id', 'status_id')
->where('user_id', $user)
->latest()
->get()
->groupBy('lesson_id')
;
如果我运行这个:
foreach ($latestUserLessons as $latestUserLesson)
{
dd($latestUserLesson);
}
我得到:
Illuminate\Database\Eloquent\Collection {#2731
#items: array:1 [
0 => App\Models\LessonUser {#2741
#guarded: []
#connection: "mysql"
#table: "lesson_users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:3 [
"user_id" => 1
"lesson_id" => 1
"status_id" => 1
]
#original: array:3 [
"user_id" => 1
"lesson_id" => 1
"status_id" => 1
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
]
}
所以我明明有 'lesson_id' 属性.
但是当我运行这个:
foreach ($latestUserLessons as $latestUserLesson)
{
dd($latestUserLesson->lesson_id);
}
我感到害怕:
Property [lesson_id] does not exist on this collection instance.
at C:\xampp\htdocs\tdd\vendor\laravel\framework\src\Illuminate\Collections\Traits\EnumeratesValues.php:850
846▕ */
847▕ public function __get($key)
848▕ {
849▕ if (! in_array($key, static::$proxies)) {
➜ 850▕ throw new Exception("Property [{$key}] does not exist on this collection instance.");
851▕ }
852▕
853▕ return new HigherOrderCollectionProxy($this, $key);
854▕ }
1 C:\xampp\htdocs\tdd\app\Http\Controllers\SeshController.php:30
Illuminate\Support\Collection::__get("lesson_id")
2 C:\xampp\htdocs\tdd\vendor\laravel\framework\src\Illuminate\Routing\Controller.php:54
App\Http\Controllers\SeshController::buildSesh()
Tests: 1 failed
Time: 1.17s
我知道如果我尝试从集合中获取 属性 会出现此错误,但这里我显然有一个模型实例。 我试过:使用 eloquent 进行数据检索;使用带有增量的 'for' 循环,直到检索到的模型计数,控制数组位移等,我只是不断收到相同的错误。
我错过了什么吗?是否有另一种获取该字段数据的方法?
啊!!求助!!
您有 collection 个 collection。您必须迭代嵌套在其中的 collection 和 collection 以获得 LessonUser 模型。
foreach ($latestUserLessons as $group) {
foreach ($group as $latestUserLesson) {
dd($latestUserLesson->lesson_id);
}
}