语法错误,意外标识符“计数”,“期望”:拉维尔9

发布于 2025-02-10 21:00:50 字数 173 浏览 1 评论 0原文

因此,我正在尝试使总体用户在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执妄 2025-02-17 21:00:50

由于卷曲支架之间的返回语句,您看到的错误被丢弃。您可以通过简单地删除返回语句来解决此问题:

{{ count(Schema::getColumnListing('users')); }}

但是,getColumnListing返回指定表中发现的所有列的数组,请参见Illuminate \ database \ database \ schema \ builder.php

/**
 * Get the column listing for a given table.
 *
 * @param  string  $table
 * @return array
 */
public function getColumnListing($table)
{
    $results = $this->connection->selectFromWriteConnection($this->grammar->compileColumnListing(
        $this->connection->getTablePrefix().$table
    ));

    return $this->connection->getPostProcessor()->processColumnListing($results);
}

要计算指定表中的行数,您可以直接从视图中访问DB对象,以下是一个示例,该示例计算从用户表中存在的行数:

{{ DB::table('users')->count(); }}

但是,可以鼓励您将此逻辑放在控制器并将适当的数据传递到一个视图(请参阅 laravel文档),例如:

usercontroller.php

<?php
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
 
class UserController extends Controller
{
    /**
     * Show a list of all of the application's users.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $numberOfUsers = DB::table('users')->count();
        return view('user.index', ['userCount' => $numberOfUsers]);
    }
}

index.blade.php(user.index)

<!-- template header goes here -->

<!-- display number of users in bold -->
We have <strong>{{ $userCount }}</strong> registered users!

<!-- template footer goes here -->

模型

,因为这样的东西(您可能有一个用户模型),我将使用db对象代替使用laravel模型。这意味着UserController的索引函数看起来像这样:

public function index()
{
    $numberOfUsers = \App\Models\User::all()->count();
    return view('user.index', ['userCount' => $numberOfUsers]);
}

最后,您可以简单地从视图调用模型:

{{ \App\Models\User::count(); }}

阅读有关模型此处

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:

{{ count(Schema::getColumnListing('users')); }}

However, getColumnListing returns an array of all columns found in the specified table, see Illuminate\Database\Schema\Builder.php:

/**
 * Get the column listing for a given table.
 *
 * @param  string  $table
 * @return array
 */
public function getColumnListing($table)
{
    $results = $this->connection->selectFromWriteConnection($this->grammar->compileColumnListing(
        $this->connection->getTablePrefix().$table
    ));

    return $this->connection->getPostProcessor()->processColumnListing($results);
}

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:

{{ DB::table('users')->count(); }}

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

<?php
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
 
class UserController extends Controller
{
    /**
     * Show a list of all of the application's users.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $numberOfUsers = DB::table('users')->count();
        return view('user.index', ['userCount' => $numberOfUsers]);
    }
}

index.blade.php (user.index)

<!-- template header goes here -->

<!-- display number of users in bold -->
We have <strong>{{ $userCount }}</strong> registered users!

<!-- template footer goes here -->

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:

public function index()
{
    $numberOfUsers = \App\Models\User::all()->count();
    return view('user.index', ['userCount' => $numberOfUsers]);
}

Finally, you could simply call the model from the view:

{{ \App\Models\User::count(); }}

Read more about models here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文