У меня есть collection of collection
в variable
. Я пытаюсь управлять данными в этой переменной, но она не работает.
// User.php
public function projects()
{
return $this->belongsToMany(Project::class,'time_entries')
->withPivot('hours');
}
// Project.php
public function users()
{
return $this->belongsToMany(User::class, 'time_entries')
->withPivot('hours');
}
/// TimeEntries.php
public function user()
{
return $this->belongsTo( 'App\Model\User' );
}
public function project()
{
return $this->belongsTo( 'App\Model\Project','project_id' );
}
Пользователь может делать много записей времени, и он может участвовать во многих проектах. Проект может иметь много временных записей.
Я пытаюсь это:
$users = User::with('projects')->get();
foreach ($users$users = User::with('projects')->get();
foreach ($users as $user) {
foreach($user->projects() as $a) {
dump ($a);
}
}
и я получил
false
дамп $ user:
User {#715 ▼
#table: "users"
#connection: "redmine"
#with: array:1 [▶]
#fillable: array:19 [▶]
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:19 [▶]
#original: array:19 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:2 [▼
"projectrecap" => Collection {#714 ▼
#items: []
}
"projects" => Collection {#1414 ▼
#items: array:11 [▼
0 => Project {#17329 ▶}
1 => Project {#17338 ▶}
2 => Project {#17339 ▶}
3 => Project {#17340 ▶}
4 => Project {#17341 ▶}
5 => Project {#17342 ▶}
6 => Project {#17343 ▶}
7 => Project {#17344 ▶}
8 => Project {#17345 ▶}
9 => Project {#17346 ▶}
10 => Project {#17347 ▶}
]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
Я бы хотел, чтобы все пользователи видели все проекты, над которыми он работал, и сумму часов, над которыми он работал.