Support the ongoing development of Laravel.io →
Views Blade
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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>
Last updated 2 years ago.
0

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
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.