Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我如何';计数';使用';其中';在拉威尔/雄辩地使用碳_Php_Mysql_Laravel_Eloquent_Php Carbon - Fatal编程技术网

Php 我如何';计数';使用';其中';在拉威尔/雄辩地使用碳

Php 我如何';计数';使用';其中';在拉威尔/雄辩地使用碳,php,mysql,laravel,eloquent,php-carbon,Php,Mysql,Laravel,Eloquent,Php Carbon,我想使用以下代码计算得到的输出: 刀片视图 这将以字符串形式提供输出,但我想计算它得到的匹配数。 我尝试了以下方法,但没有成功: {{ $kentekens->where('created_at', '>=', Carbon::today()->count()) }} {{ $kentekens->count()->where('created_at', '>=', Carbon::today()) }} 控制器 模型 有人知道吗?正确的语法是: {{ $

我想使用以下代码计算得到的输出:

刀片视图 这将以字符串形式提供输出,但我想计算它得到的匹配数。 我尝试了以下方法,但没有成功:

{{ $kentekens->where('created_at', '>=', Carbon::today()->count()) }}

{{ $kentekens->count()->where('created_at', '>=', Carbon::today()) }}
控制器 模型 有人知道吗?

正确的语法是:

{{ $kentekens->where('created_at', '>=', Carbon::today())->count() }}

问题1

一种解决方案是从控制器向视图添加两个变量:

控制器

public function create() {
    $kentekensQuery = Kenteken::latest()->where('created_at', '>=', Carbon::today());

    return view('layouts.dashboard')
        ->with('kentekens', $kentekensQuery->get())
        ->with('kentekensCount', $kentekensQuery->count());
}
public function create() {
    $kentekens = Kenteken::latest()->where('created_at', '>=', Carbon::today();

    return view('layouts.dashboard')
        ->with('kentekens', $kentekens->get());
}
$kentekensQuery = Kenteken::latest()
     ->where('created_at', '>=', Carbon::today())
     ->where('kenteken', 'LIKE', 'B%');
查看

{{ $kentekens }}
{{ $kentekensCount }}
{{ $kentekens }}
{{ $kentekens->count() }}
{{
    $kentekens->filter(function ($value, $key) {
        return strpos($value, 'B') === 0;
    });
}}
但是这个方法会产生两个sql请求:第一个用于获取项目,第二个用于计数项目

更好的解决方案可能是只返回第一个请求的结果作为一个集合,并对该集合调用该方法。事实上,在雄辩的模型查询生成器上调用的get()方法返回一个集合\o/

控制器

public function create() {
    $kentekensQuery = Kenteken::latest()->where('created_at', '>=', Carbon::today());

    return view('layouts.dashboard')
        ->with('kentekens', $kentekensQuery->get())
        ->with('kentekensCount', $kentekensQuery->count());
}
public function create() {
    $kentekens = Kenteken::latest()->where('created_at', '>=', Carbon::today();

    return view('layouts.dashboard')
        ->with('kentekens', $kentekens->get());
}
$kentekensQuery = Kenteken::latest()
     ->where('created_at', '>=', Carbon::today())
     ->where('kenteken', 'LIKE', 'B%');
查看

{{ $kentekens }}
{{ $kentekensCount }}
{{ $kentekens }}
{{ $kentekens->count() }}
{{
    $kentekens->filter(function ($value, $key) {
        return strpos($value, 'B') === 0;
    });
}}
问题2

使用上述第一种解决方案:

控制器

public function create() {
    $kentekensQuery = Kenteken::latest()->where('created_at', '>=', Carbon::today());

    return view('layouts.dashboard')
        ->with('kentekens', $kentekensQuery->get())
        ->with('kentekensCount', $kentekensQuery->count());
}
public function create() {
    $kentekens = Kenteken::latest()->where('created_at', '>=', Carbon::today();

    return view('layouts.dashboard')
        ->with('kentekens', $kentekens->get());
}
$kentekensQuery = Kenteken::latest()
     ->where('created_at', '>=', Carbon::today())
     ->where('kenteken', 'LIKE', 'B%');
对于第二种解决方案,正如@Alexei Mezenin所说,您必须使用闭包,即在使用函数迭代集合上的每个值时运行的函数,这里的函数:

查看

{{ $kentekens }}
{{ $kentekensCount }}
{{ $kentekens }}
{{ $kentekens->count() }}
{{
    $kentekens->filter(function ($value, $key) {
        return strpos($value, 'B') === 0;
    });
}}

接下来的问题是,如果我还想计算他们需要特定首字母的位置,该怎么办。我看到了类似的东西,但没有起作用:{{$kentekens->where('created_at','>=',Carbon::today())->where('kenteken','like','B%')->count()}}@JesseyFransen您只能在查询数据库时使用
like
。当你在这里处理一个集合时,你需要使用带有闭包的
filter()
方法。我还是一个新手。你能给我一个更详细的回答吗?