Home > Mobile >  need help logic if and loop laravel
need help logic if and loop laravel

Time:12-15

I'm planning to do code confirmation in a program where it requires 2 or more codes and confirmation.I tried and stuck with code like this

@foreach ($kode as $k)
    @if ($k->kode != $k->konfirmasi )
        <form action="/tps/kode" method="post">
            @csrf
                <input type="text" name="{{ $k->status }}" id="{{ $k->status }}" placeholder="{{ $k->status }}">
                <button type="submit">submit</button>
        </form>
        {{-- @break --}}


        @else
            succes



    @endif

@endforeach

I tried to use break and continue but that's not the place, maybe. It seems more to the logic where if the konfirmasi is the same and everything is the same then it will be successful. but I'm confused what to use

CodePudding user response:

If I understand your question correctly, then you should first validate the codes and after iterating over the $kode variable, check if a kode was not the same as the konfirmasi. If so, show the form.

@php($failure = false)
@foreach ($kode as $k)
    @if ($k->kode != $k->konfirmasi )
        @php
            $failure = true;
            $failedKode = $k;
        @endphp
    @endif
@endforeach


@if($failure)
    <form action="/tps/kode" method="post">
        @csrf
        <input type="text" name="{{ $failedKode->status }}" id="{{ $failedKode->status }}" placeholder="{{ $failedKode->status }}">
        <button type="submit">submit</button>
    </form>
@endif

Although this is a solution, let me advise you, like @TimLewis mentioned, move this kind of logic to the controller. This logic doesn't belong in views.

  • Related