Yes, by not doing it. Your view should not need to have variables set within it. Your data you pass to it should be ready for your view to present to the user.
If it is about setting variables in a view, you shouldn't do that, but if you want you cold do this:
<?php $wp = ''; ?>
<tbody>
@foreach($tasks as $task)
@if($wp != $task->workpackage->id)
<tr>
<td colspan="3">{{{ $task->workpackage->description }}}</td>
</tr>
<?php $wp = $task->workpackage->id ?>
@endif
<tr>
<td>{{{ $task->reference }}}</td>
<td>{{{ $task->description }}}</td>
<td></td>
</tr>
@endforeach
</tbody>
You could group your tasks by workpackage and then pass that to the view:
@foreach($workPackages as $workPackage)
<tr>
<td colspan="3">{{{ $workPackage->description }}}</td>
</tr>
@foreach($workPackage->tasks as $tast)
<tr>
<td>{{{ $task->reference }}}</td>
<td>{{{ $task->description }}}</td>
<td></td>
</tr>
@endforeach
@endforeach
You could also create a nested array structured like this:
[
'package1' => [
'package' => (the WorkPackage instance),
'tasks' => [ (the Task instances) ]
]
Which would then allow for almost the same thing
@foreach($workPackages as $workPackage)
<tr>
<td colspan="3">{{{ $workPackage['package']->description }}}</td>
</tr>
@foreach($workPackage['tasks'] as $task)
<tr>
<td>{{{ $task->reference }}}</td>
<td>{{{ $task->description }}}</td>
<td></td>
</tr>
@endforeach
@endforeach
thepsion5 said:
You could group your tasks by workpackage and then pass that to the view:
After the first reply from matthewburrow I realised that I asked before thinking a bit. So I was already implementing this when reading his suggestion.
Thanks a lot everyone.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community