I’m trying to use Laravel each method inside a with in my query builder but the attribute I’m adding inside my each method does not persist to the collection. My attribute does show when I dd($task);
inside the each method.
$projects =
Project::with(['tasks' => function($query) {
$query
->whereBetween('start_date', ['2024-04-21', '2024-04-28'])
->orWhereBetween('end_date', ['2024-04-21', '2024-04-28'])
->each(function($task, $key) {
$task->test = 'test';
$task->setAttribute('date', $task->end_date);
// dd($task); <- this returns the 'test' and 'date' attributes above
});
}])
->status(['Active', 'Scheduled'])->sortByDesc('last_status.start_date');
// dd($projects->first()->tasks->first()); <- this is missing the 'test' and 'date' attributes from above