如何过滤掉以特定首字母开头的列表(laravel/eloquent)

Jes*_*sen 1 php laravel eloquent php-carbon

我有一个字符串列表,只想获取以字母“B”开头的字符串。我尝试过以下代码但没有成功。

{{ $kentekens->where('kenteken', 'LIKE', 'B%') }}
Run Code Online (Sandbox Code Playgroud)

{{ $kentekens->filter(function ($value, $key) {
        return strpos($value, 'B') === 0;
    });
}}
Run Code Online (Sandbox Code Playgroud)

控制器

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

return view('layouts.dashboard', compact('kentekens'));
Run Code Online (Sandbox Code Playgroud)

有人知道正确的语法吗?

Ale*_*nin 5

如果您只想获取从 B 开始的数据,请执行以下操作:

Kenteken::latest()
    ->where('kenteken', 'like', 'B%')
    ->where('created_at', '>=', Carbon::today())
    ->get();
Run Code Online (Sandbox Code Playgroud)