【问题标题】:@if statement doesn't works@if 语句不起作用
【发布时间】:2021-04-23 05:38:46
【问题描述】:

晚上好,当用户数据库中没有注册时,我正在尝试制作一个显示重定向按钮的登录页面。当数据库中有一个或多个寄存器时,显示所有用户以选择其中一个。问题是当数据库有寄存器时视图不会改变。

这里有刀片模板代码:

@if(empty($users))
                    <div class="grid-item" id="grid-item5">
                        <div id="title">
                            <h1>Welcome</h1>
                            <p>We see there aren't users</p>
                        </div>
                        <div id="loginForm">
                            <button type="button" onclick="window.location='{{ url("/newUser") }}'">Create user</button>
                        </div>
                    </div>
                @else
                    <div class="grid-item" id="grid-item5">
                        <div id="title">
                            <h1>Select an user</h1>
                        </div>
                        <div id="loginForm"></div>
                    </div>
                @endif

这里有控制器代码:

public function index()
    {
        $users = User::all();
        return view('users.index')->with('users', $users);
    }

这里有 web.php 路由:

Route::get('/', function () {
    return view('landing');
});

Route::get('/newUser', function () {
    return view('createUser');
});

Route::resource('users', UserController::class);

谁能帮帮我?不知道怎么解决...

【问题讨论】:

  • 对不起,我之前没听过populate这个词(英语不是我的母语),请问你能用其他词重新表述这个问题吗?
  • 既然你使用(我猜)laravel Collection,你应该使用 $users->isEmpty() 而不是 empty($users)。如果您希望用户作为数组,请使用 $users->toArray();

标签: php if-statement laravel-blade laravel-8


【解决方案1】:

empty($users)$users = User::all() 的结果不兼容。 ::all() 返回一个Collection,这是一个Object

Illuminate\Database\Eloquent\Collection {#3357
  all: [],
}

在此Object 上使用empty($users) 将返回false。您可以使用多个Collection 方法或PHP 方法count() 来确定:

@if($users->count() == 0)
# OR
@if(!$users->count())
# OR
@if(count($users) == 0)
# OR
@if(!count($users))
# OR 
@if($users->isEmpty())
# Etc etc...

为了清楚起见,我建议使用-&gt;isEmpty(),但我提供了所有这些以显示不同的方法。

这里有很多有用的方法可供使用:

https://laravel.com/docs/8.x/collections#available-methods

编辑:如果同一个视图被多次渲染,$users 需要可用,或者通过isset() 进行检查:

@if (!isset($users) || $users->isEmpty())
<div class="grid-item" id="grid-item5">
  <div id="title">
    <h1>Welcome</h1>
    <p>We see there aren't users</p>
  </div>
  <div id="loginForm">
    <button type="button" onclick="window.location='{{ url("/newUser") }}'">Create user</button>
  </div>
</div>
@else
<div class="grid-item" id="grid-item5">
  <div id="title">
    <h1>Select an user</h1>
  </div>
  <div id="loginForm"></div>
</div>
@endif

【讨论】:

  • 如果我这样做,laravel 会抛出“未定义变量:用户”错误:/
  • 那是因为你的变量是$users,而不是$user。检查你的拼写。此外,如果您在多个地方使用 view('index', ...),请确保每次都传递相同的变量。
  • 刚刚意识到我将第一张支票写为$user;道歉。一个好的做法是永远不要盲目地复制和粘贴代码;检查它是否正确并且不会破坏其他任何东西,并使用 IDE :)
  • 好吧,它仍然抛出错误,我正在考虑创建一个 LoginPageController 以在该控制器中创建一个函数并删除刀片中的 @if
  • 查看我更新的代码;我怀疑您有多个return view('users.index') 实例,并且您没有为每个实例传递变量。您可以通过isset() 处理此问题,或者确保您在每个邮件上都传递-&gt;with('users', $users)
【解决方案2】:

已解决,问题是刀片文件名的路由问题。我把它放在登录文件夹中,并在 web.php 中更改了路由

无论如何,感谢大家花费了一点时间来尝试 h

【讨论】:

    猜你喜欢
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2017-10-22
    • 2018-05-11
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多