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.

推荐答案

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

现在,如果我们将其与Laravel框架的源代码进行比较,看看循环是如何编译的(当PHP在网页上呈现时,所有Blade struct 都被编译成HTML):

   /**
     * 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.


参考链接:

  1. Blade Templates
  2. Blade View Compile Functions

Laravel相关问答推荐

Inertia React中的错误文件上传更新

@vite指令在使用laravel vite构建后导致错误

Laravel Livewire与自定义JS有关的多姆问题

Laravel中如何动态更改路由链接?

如何在Laravel中创建自定义单项集合

阻止用户查看其他用户正在查看的内容的最佳方法是什么?

Laravel 5.2 无法打开 laravel.log

Laravel 4:如何将 WHERE 条件应用于 Eloquent 类的所有查询?

如何比较laravel中的两个加密(bcrypt)密码

错误:您的要求无法解析为一组可安装的软件包.

Laravel 将参数从路由传递到过滤器

如何在 Laravel 5.5 中为选定的请求类设置自定义响应

调用未定义的方法 Maatwebsite\Excel\Excel::load()

Laravel 项目旁边的 Wordpress 项目(在 public_html 文件夹中)

将自定义消息(或任何其他数据)传递给 Laravel 404.blade.php

在 Laravel 中结合 AND/OR Eloquent的查询

Laravel 5 事件处理程序未触发

在 laravel Passport 客户端应用程序中使用访问令牌获取用户数据

在 Laravel 中软删除父记录时如何软删除相关记录?

Laravel Eager加载与显式连接