0

I've just started using the framework. In plain PHP after the opening foreach I would then set the variables then close the php tag but then from what I can work out you have to then do the Laravel @foreach tags and then open and close @php. Is there a way around this as it seems like a lot of extra work and code?

        @foreach($steps as $row)
            @php
                $title = $row->title;
                $text = $row->text;
                $i = 1;
            @endphp
            <div class="steps-item grid-wrap">
                <div class="number"
                @if($text || $title)
                    <div class="text-wrap">
                        @if($title)
                            <h2>{{$title}}</h2>
                        @endif
                        {!! $text !!}
                    </div>
                    @php
                        $i++;
                    @endphp
                @endif
            </div>{{--END steps-item--}}
        @endforeach
1
  • 2
    Couldn't/shouldn't this be done in the controller or model? Commented Jun 24, 2021 at 13:06

4 Answers 4

2

Since blade is no PHP, you have to return to PHP with that directive. But you can set/use the variables without doing that in your case:

@foreach($steps as $i => $row)
    <div class="steps-item grid-wrap">
        <div class="number"
        @if($text || $title)
            <div class="text-wrap">
                @if($title)
                    <h2>{{ $row->title }}</h2>
                @endif
                {!! $row->text !!}
            </div>
            @php
                $i++;
            @endphp
        @endif
    </div>{{--END steps-item--}}
@endforeach

If you still want to set variables, there's a Laravel package called alexdover/blade-set. But as @brombeer pointed out, in most cases it's highly recommended to set all necessary variables in the controller before passing them to the view.

Sign up to request clarification or add additional context in comments.

Comments

1

Use laravel provided loop variables:

$loop->iteration    The current loop iteration (starts at 1).

It will increment in every loop iteration automatically.

e.g:
First iteration = $loop->iteration => 1 ;
Second iteration = $loop->iteration => 2 ;
so on until loop ends.

Check docs: The Loop Variables

Comments

0

You can use a @for directive with sizeof($steps) like that:

@for($i=0; $i<= sizeof($steps)-1; $i++)

@endfor

Comments

0

@foreach ($steps as $row)

<div class="steps-item grid-wrap">
    <div class="number">
        <div class="text-wrap">
            @if ($row->title != '')
                <h2>{{$row->title}}</h2>
               /* if you want to display another title when its blank you can 
                use if-else here otherwise not need to use if conditions for 
                title and text */
            @endif
            @if ($row->text != '')
               {!! $row->text !!}
            @endif
        </div>
    </div>
</div>

@endforeach

{{-- for an example you have $steps values like

$steps = Array( [0] -> 1, [1] -> 'title', [2] -> 'text' );

if you want this array key value pair you have to use @foreach like @foreach ($steps as $key=>$value) @endforeach

you can use key value in @foreach loop only --}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.