使用like和wildcard过滤集合

时间:2017-09-29 10:02:40

标签: php laravel laravel-5 laravel-5.5

我需要获取以所需字母开头的所有文件,我正在尝试使用->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运算符来过滤集合吗?谢谢你们!

1 个答案:

答案 0 :(得分:6)

确实laravel集合中没有 $collect = User::all()->filter(function($user){ return starts_with($user->name, 'T'); }); ,但因为where函数还在后台使用过滤器,如评论中所述,您可以根据需要构建过滤器。

以下示例说明过滤器返回名称以T

开头的用户
select PassengerID
from (
select PassengerID, count(*) as c
from yourtable
group by PassengerID, CompanyID) t
group by PassengerID
having min(c) = max(c) and count(*) > 1

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

  

PS通常你会在提取数据时提交db来进行过滤(至少对于我刚给出的例子)