0

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

1
  • I'm confused, the code above can, but if one of the first conditions fails then go straight to the second condition. What I want is the first condition to finish first and then enter the second condition Commented Dec 14, 2022 at 14:54

1 Answer 1

1

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.

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

9 Comments

Agreed with this, but this kind of logic should really be done in the Controller where $kode is being defined. Views should be for display, but you're iterating and defining variables which are then used for displaying the <form>. Also, you're using $k outside of the foreach(); PHP allows this, but $k will be the last $kode that was iterated, which without any kind of break statement, will simply be the last element in $kode, and not the element that triggered the $failure toggle.
Absolutely agree with this, but without having more context or knowledge about the project/code, this is a quick answer. I've also updated my answer to be able to use the $k outside this loop. Thanks for noticing it
yes, this is close. Maybe my explanation is lacking, but this is correct. It's just that I plan to input confirmation first(1), then enter the (2) second condition (the condition that enters when the first condition / input code has all been entered)
That's definitely better, and yeah, I understand the reasoning for answering this question. Quick answers can be useful (and I think this one is, even barring the fact this should be in a different spot), but be careful with "quick answers"; even with the best intentions, you can still get downvoted simply for trying to answer a question that isn't in an answerable state.
but how to do it when done in the controller??. I need knowledge from you
|

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.