stntmnky
stntmnky

Reputation: 37

How do I check if a variable is defined when it is being used in a @foreach loop

I'm trying to get some clarification on this, as I've tried different methods and it doesn't seem to have worked for me.

I have a foreach loop like so:

@foreach($mods as $key => $value)
                <tr class="table-tr draggable" data-id="{!!$value->id!!}" style="background-color: {{$value->mod_colour}}; color: {{$value->mod_text_colour}};">
                    <!-- This creates the Mod -->
                    <td class="startTime"></td>
                    <td class="endTime"></td>
                    <td class="duration">{{ $value->mod_duration }}</td>
                    <td class="intent">{{ $value->mod_intent }}</td>
                    <td class="module">
                        @if ($value->mod_module != null)
                            {{ $value->mod_module }}
                        @else
                            <table class="table-bordered" style="width:100%; text-align: center; color: white;">
                                @for ($i = 1; $i <= $value->mod_team; $i++)
                                    <td style="padding:10px;">Team {{ $i }}</td>
                                @endfor
                            </table>
                        @endif
                    </td>
                    <td class="output">{{ $value->mod_output }}</td>
                    <td class="input">{{ $value->mod_input }}</td>
                    <td class="logistics">{{ $value->mod_logistics }}</td>
                    <td>
                        <a class="btn btn-small btn-info" style="margin:auto; margin-top: 10px; margin-bottom: 10px; margin-left:10px; margin-right:10px; display:block;" href="" data-target="#modal-edit-mod{{ $value->id }}" data-toggle="modal" id="modal-edit">Edit</a>
                    </td>
                </tr>
@endforeach

The problem that I'm facing is that when there are no mods in the database, and therefore there is nothing being passed to the $value variable, the page will throw a 'Undefined variable: value' error. What I'm trying to achieve is to create some sort of catch statement, where if the value is not defined then it'll default to a 'Create New Mod' screen or modal. I've tried the following:

@foreach($mods as $key => $value)
                @if($value != null)
                <tr class="table-tr draggable" data-id="{!!$value->id!!}" style="background-color: {{$value->mod_colour}}; color: {{$value->mod_text_colour}};">
                    <!-- This creates the Mod -->
                    <td class="startTime"></td>
                    <td class="endTime"></td>
                    <td class="duration">{{ $value->mod_duration }}</td>
                    <td class="intent">{{ $value->mod_intent }}</td>
                    <td class="module">
                        @if ($value->mod_module != null)
                            {{ $value->mod_module }}
                        @else
                            <table class="table-bordered" style="width:100%; text-align: center; color: white;">
                                @for ($i = 1; $i <= $value->mod_team; $i++)
                                    <td style="padding:10px;">Team {{ $i }}</td>
                                @endfor
                            </table>
                        @endif
                    </td>
                    <td class="output">{{ $value->mod_output }}</td>
                    <td class="input">{{ $value->mod_input }}</td>
                    <td class="logistics">{{ $value->mod_logistics }}</td>
                    <td>
                        <a class="btn btn-small btn-info" style="margin:auto; margin-top: 10px; margin-bottom: 10px; margin-left:10px; margin-right:10px; display:block;" href="" data-target="#modal-edit-mod{{ $value->id }}" data-toggle="modal" id="modal-edit">Edit</a>
                    </td>
                </tr>
                @else
                <p>No mods!</p>
                @endif
            @endforeach

where it'll check if the $value variable is null or not, and that hasn't worked. I've also tried the ->isEmpty() method, but it still returns with the undefined variable error.

What I'd like help with is a pointer on the best way to create a @if/@else statement where it can check if the $value variable has a value or not, or if there is a better way to achieve what I'm trying to do.

Thank you!

Upvotes: 1

Views: 152

Answers (6)

ATechGuy
ATechGuy

Reputation: 1270

you can do this in laravel. Id check your result of $mods to see if anything is in it, then execute if need be:

@if(is_null($mods))
  //its null, do this code
@else
your current code if a var is found
@endif

Upvotes: 0

Caio C&#233;sar Brito
Caio C&#233;sar Brito

Reputation: 167

You can use the isset() method!

Something like:

if (isset($var)) {}

If $var is set, the method return true, if not, return false.

Good luck!

Upvotes: 1

Arigi Wiratama
Arigi Wiratama

Reputation: 446

To make it more clearly, problem about the variable is defined or not is different with the variable is null or not, and there's different way too to check it.

Here's I give you little example.

// create variable empty and fill it with null/''/0

$empty = null; // I set it with null

// And we create another variable notEmpty
$notEmpty = 'Not Empty';

if(empty($empty)) {
    echo "Empty"; // It will echo Empty if we run it
}

if(empty($notEmpty)) {
    echo "Empty"; // Not called
} else {
    echo "Not Empty"; // Called
}

// Now checking if variable is defined or not
if(isset($otherVar)) {
   echo "Defined";
} else {
   echo "is Not Defined";
}

As you can see for your problem you can choose from that 2 method :-D

Upvotes: 0

Shikhar Tandon
Shikhar Tandon

Reputation: 154

You should use isset($value) to check if the variable is set or not.

Also in such cases you could use both to check if variable is set and not empty

if(isset($value)) && is empty($value))
{
    \\ Your Code
}

Upvotes: 0

RJParikh
RJParikh

Reputation: 4166

You can use isset() and empty().

@foreach($mods as $key => $value)
    @if(isset($value)) 

OR

@foreach($mods as $key => $value)
    @if(!empty($value)) 

Upvotes: 3

David
David

Reputation: 354

To check if a variable is defined is with isset(), for example:

if(isset($value)){
   echo $value;
}

Upvotes: 0

Related Questions