语法错误,意外标识符“计数”,“期望”:拉维尔9
因此,我正在尝试使总体用户在Laravel 9中计数,但是当我完成它时,我得到了此错误语法错误,意外的标识符“计数”,期待“:”
这是代码code> {{return count(schema :: getColumnListing('用户')); }}
So I'm trying to make a total users count in laravel 9 but when I finished with it I got this error syntax error, unexpected identifier "count", expecting ":"
Here's the code {{ return count(Schema::getColumnListing('users')); }}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于卷曲支架之间的返回语句,您看到的错误被丢弃。您可以通过简单地删除返回语句来解决此问题:
但是,
getColumnListing
返回指定表中发现的所有列的数组,请参见Illuminate \ database \ database \ schema \ builder.php
:要计算指定表中的行数,您可以直接从视图中访问DB对象,以下是一个示例,该示例计算从用户表中存在的行数:
但是,可以鼓励您将此逻辑放在控制器并将适当的数据传递到一个视图(请参阅 laravel文档),例如:
usercontroller.php
index.blade.php(user.index)
模型
,因为这样的东西(您可能有一个用户模型),我将使用db对象代替使用laravel模型。这意味着UserController的索引函数看起来像这样:
最后,您可以简单地从视图调用模型:
阅读有关模型此处。
The error you’re seeing is thrown due to the return statement between the curly braces. You can fix this by simply removing the return statement:
However,
getColumnListing
returns an array of all columns found in the specified table, seeIlluminate\Database\Schema\Builder.php
:To count the number of rows within a specified table you could access the DB object directly from a view, here is an example which counts the number of rows present from the users table:
However, you may be encouraged to put this logic within a controller and pass the appropriate data to a view (see the Laravel documentation), for example:
UserController.php
index.blade.php (user.index)
Models
Personally, for something like this (where you’re likely to have a User model) I would substitute using the DB object with using a Laravel model. This means the index function from UserController would look something like this:
Finally, you could simply call the model from the view:
Read more about models here.