Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel 8 pass variable to be used in foreach loop for comparison

I’m trying to pass a predefined variable with a value into the @if part of the statement for comparison. This is for Laravel v8.0 on the blade.php. It only works if I hardcoded the value in it but not through the variable $value. I would need to have a placeholder variable to pass the value for comparison, as it will be inputted from user. Hence may I know what is the proper way to declare the variable as in this case? Sorry I’m just starting learning Laravel. Thanks for your help.

Eg:

    $value = "abc123";

    @foreach($surveys as $survey)
        @if($survey->Unit_code == {{$value}})
            
            <form action="" method="">
                @csrf 
                <div class="form-group">
                    <label for="name">Name</label> <br>
                    <input type="text" name="Name" class="form-control" value="{{$survey->name}}">
                </div>

                <input type="submit" value="Save">
            </form>
        @endif
    @endforeach 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just so this is in an answer form for other users that encounter this problem as a developer just learning laravel; the issue here is there was an output to the dom using {{}} where the blade directives (things that start with @) use the PHP vars as is an example of this would be as follows:

@if($survey->Unit_code == $value)

the other issue he was having, in this case, is assigning raw PHP on the blade file, this works the same way. Although, to add raw PHP to the dom you would use the @php directive

    @php
        $value = "abc123";
    @endphp
    @foreach($surveys as $survey)
        @if($survey->Unit_code == $value)
            
            <form action="" method="">
                @csrf 
                <div class="form-group">
                    <label for="name">Name</label> <br>
                    <input type="text" name="Name" class="form-control" value="{{$survey->name}}">
                </div>

                <input type="submit" value="Save">
            </form>
        @endif
    @endforeach

if the data of $value needed to be completely dynamic based on the backend then I would recommend passing it to the blade file via the view() function instead and assigning the value there or as nullable on the first pass of the function. Hope that helps!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading