Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between foreach and forelse in Laravel

What's the basic difference between Laravel's foreach and forelse? How does forelse work?

I know that foreach duplicates your given array and works on it while in forelse it checks whether it exists and loops on it if it exists but if not it uses your provided else condition.

like image 701
Gaurav Gupta Avatar asked Dec 26 '17 13:12

Gaurav Gupta


People also ask

What is the difference between foreach and forelse?

I know that foreach duplicates your given array and works on it while in forelse it checks whether it exists and loops on it if it exists but if not it uses your provided else condition.

What is Forelse PHP?

The PHP foreach Loop The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

What is foreach laravel?

Laravel blade has a foreach directive that we can use the same way as we use the foreach loop in PHP. @foreach directive is more powerful than a normal foreach loop because of the $loop variable that is available inside every $foreach loop.

How can we create conditionals loop in laravel?

To loop the OR condition in Laravel Eloquent you can call the WHERE condition and passing a closure (anonymous function) to the method. By doing so you can execute your loop within the function which then will continue chaining the OR conditions.


3 Answers

I believe the answer to your question is that, essentially, ForElse is a ForEach loop, but with extra handling for empty array.

From the Laravel 5 docs on Blade Templates, an example illustrating both loops with the same list of Users as Input:

@foreach ($users as $user)
    <p>This is user {{ $user->id }}</p>
@endforeach

@forelse ($users as $user)
    <li>{{ $user->name }}</li>
@empty
    <p>No users</p>
@endforelse

Now if we compare this to the source code for the Laravel Framework to see how the loops are compiled (all Blades constructs are compiled into HTML when being rendered by PHP on the web page):

   /**
     * Compile the for-each statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileForeach($expression)
    {
        preg_match('/\( *(.*) +as *(.*)\)$/is', $expression, $matches);

        $iteratee = trim($matches[1]);

        $iteration = trim($matches[2]);

        $initLoop = "\$__currentLoopData = {$iteratee}; \$__env->addLoop(\$__currentLoopData);";

        $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();';

        return "<?php {$initLoop} foreach(\$__currentLoopData as {$iteration}): {$iterateLoop} ?>";
    }


   /**
     * Compile the for-else statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileForelse($expression)
    {
        $empty = '$__empty_'.++$this->forElseCounter;

        preg_match('/\( *(.*) +as *(.*)\)$/is', $expression, $matches);

        $iteratee = trim($matches[1]);

        $iteration = trim($matches[2]);

        $initLoop = "\$__currentLoopData = {$iteratee}; \$__env->addLoop(\$__currentLoopData);";

        $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();';

        return "<?php {$empty} = true; {$initLoop} foreach(\$__currentLoopData as {$iteration}): {$iterateLoop} {$empty} = false; ?>";
    }

Essentially, the ForElse loop has a built-in code and compile check for an empty input, in which case it optionally outputs an alternate display template for the empty input.

I would imagine you can use a ForEach loop in every case that you might use a ForElse loop, adding that you include and empty checks that ForElse might catch for you.


Links for reference:

  1. Blade Templates
  2. Blade View Compile Functions
like image 68
RoboBear Avatar answered Oct 10 '22 15:10

RoboBear


The foreach loop as you described loops over each element in an array, the foresee loop in essence is exactly the same with one extra element as you already noticed, the else statement. The primary use for this to use when you have an empty array.

Thus looping over all elements and for example rendering a row of a table but putting an empty notice when you do not have any data.

like image 42
milo526 Avatar answered Oct 10 '22 14:10

milo526


See the basic difference is in case of no data found foreach loop you have to check count condition and then print as per output. This will increase little bit of effort but in forelse it auto check and using @empty you have to echo no data found..

like image 36
Akshay Bhardwaj Avatar answered Oct 10 '22 15:10

Akshay Bhardwaj