gpt4 book ai didi

php - 使用 like 和通配符过滤集合

转载 作者:可可西里 更新时间:2023-10-31 22:40:13 26 4
gpt4 key购买 nike

我需要获取所有以所需字母开头的文件,我正在尝试使用 ->where 过滤器添加 'like' 作为运算符,但通配符不会'不工作。

        $files = File::files(storage_path($id_files));
$files = collect($files);
$files->transform(function ($item, $key){
$item->public_filename = $item->getFilename();
return $item;
});

这是我们的目标数据,我需要创建一个 public_filename 字段来应用过滤器。我们的文件(dd($files)):

   Collection {#503 ▼
#items: array:3 [▼
0 => SplFileInfo {#525 ▼
-relativePath: ""
-relativePathname: "atxt.txt"
path: "/home/vagrant/Code/project/storage/app/uploads/general"
filename: "atxt.txt"
basename: "atxt.txt"
pathname: "/home/vagrant/Code/project/storage/app/uploads/general/atxt.txt"
extension: "txt"
realPath: "/home/vagrant/Code/project/storage/app/uploads/general/atxt.txt"
aTime: 2017-09-29 09:51:17
mTime: 2017-09-27 14:39:11
cTime: 2017-09-27 14:39:11
inode: 32833
size: 3
perms: 0100777
owner: 1000
group: 1000
type: "file"
writable: true
readable: true
executable: true
file: true
dir: false
link: false
}
1 => SplFileInfo {#524 ▼
-relativePath: ""
-relativePathname: "batxt.txt"
path: "/home/vagrant/Code/project/storage/app/uploads/general"
filename: "batxt.txt"
basename: "batxt.txt"
pathname: "/home/vagrant/Code/project/storage/app/uploads/general/batxt.txt"
extension: "txt"
realPath: "/home/vagrant/Code/project/storage/app/uploads/general/batxt.txt"
aTime: 2017-09-29 09:51:31
mTime: 2017-09-27 14:39:11
cTime: 2017-09-27 14:39:11
inode: 32834
size: 3
perms: 0100777
owner: 1000
group: 1000
type: "file"
writable: true
readable: true
executable: true
file: true
dir: false
link: false
}
2 => SplFileInfo {#526 ▼
-relativePath: ""
-relativePathname: "txt.txt"
path: "/home/vagrant/Code/project/storage/app/uploads/general"
filename: "txt.txt"
basename: "txt.txt"
pathname: "/home/vagrant/Code/project/storage/app/uploads/general/txt.txt"
extension: "txt"
realPath: "/home/vagrant/Code/project/storage/app/uploads/general/txt.txt"
aTime: 2017-09-27 14:39:11
mTime: 2017-09-27 14:39:11
cTime: 2017-09-27 14:39:11
inode: 5438
size: 3
perms: 0100777
owner: 1000
group: 1000
type: "file"
writable: true
readable: true
executable: true
file: true
dir: false
link: false
}
]
}

我正在尝试:

dd($files->where('public_filename','like','t%')); // 0 results

dd($files->where('public_filename','like','txt.txt')); //If I ommit wildcard and look for full name it retrieves correct file

所以我们的目标是:

dd($files->where('public_filename','like','t%')); // 1 result

有什么想法吗?我们可以使用通配符通过 like 运算符来过滤集合吗?谢谢大家!

最佳答案

确实在 laravel 集合中没有 where like 但是因为 where 函数也在后台使用过滤器,正如评论中所说,您可以根据需要构建过滤器。

以下示例过滤返回名称以 T 开头的用户

    $collect = User::all()->filter(function($user){
return starts_with($user->name, 'T');
});

Laravel 还有几个函数可以帮助操作和使用字符串,例如 ends_with()、str_contains() 等。其他 php 字符串操作函数可以帮助您。

PS Generally you'll want to commit your db to do the filtering when fetching the data (at least for the example I just gave)

关于php - 使用 like 和通配符过滤集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46486470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com