我想過濾一堆以字符開頭,后跟通配符*的名稱查詢我可以通過查詢來實現這一點,例如return $q->where('name', 'like', $name . '%');由于我已緩存 modal::class,因此我不想重復它,而是使用filter()或其他可以幫助我獲得預期結果的方法。集合過濾器()return $collection->filter(function ($q) use ($name) {? ? return false !== stripos($q['name'], $name); // this returns all the names that contains $name character});我想要實現的是過濾()以特定字符開頭然后是“%”的名稱 -$name . '%'例如'A%'
1 回答

慕的地10843
TA貢獻1785條經驗 獲得超8個贊
你可以使用這個Str::startsWith
助手。
use Illuminate\Support\Str;
$result = Str::startsWith('This is my name', 'This');
// true
應用到你的代碼,它應該是
use Illuminate\Support\Str;
return $collection->filter(function ($q) use ($name) {
? ? return Str::startsWith($q['name'], $name);
});
對于 5.7 之前的 laravel 版本,請改用starts_with
helper。
$result = starts_with('This is my name', 'This');
// true
- 1 回答
- 0 關注
- 102 瀏覽
添加回答
舉報
0/150
提交
取消