2017-09-29 8 views
2

希望の文字で始まるすべてのファイルを取得する必要があります。->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 

任意のアイデア?類似の演算子を使用してコレクションをフィルタリングするためにワイルドカードを使用できますか?君たちありがとう!

+0

コレクション、あなたは可能 –

+0

ワイルドカードなしで動作します:( – aaron0207

+0

これはデフォルトでは完全に一致しているため、ここでソースコードを確認できます:https://github.com/laravel/framework/blob/5.5/src/イルミネイト/サポート/ Collection.php#L482 –

答えて

2

実際には、laravelコレクションにはwhere likeがありませんが、where関数はバックグラウンドでfilterも使用するため、コメントに記載されているように、必要に応じてフィルタを構築できます。

例は、名前がTで始まることフィルタ返さ次ユーザである

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

Laravelはまたなど他のPHP操作などends_with(AS文字列を使用することができ、いくつかの機能)、str_contains()を有しています文字列操作関数はあなたを助けるためのものです。

PS一般的には、データを取得する際に(少なくとも私はちょうど与えたなど)のフィルタリングを行うには、あなたのデシベルをコミットしたいと思うオペレータのように持っていません

+1

ええ、それはDBの作業に使用されますが、私はファイルで作業しています。それでおしまい!それは働いている – aaron0207

関連する問題