2017-09-29 50 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功能在後臺還使用過濾器,正如評論中所述,您可以根據需要構建過濾器。

一個例子是下面的該過濾器返回的用戶的名字以T開頭

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

Laravel還具有多種功能,可以幫助操作和使用字符串,例如ENDS_WITH(),str_contains()等其他PHP字符串操作函數可以幫助你。

PS一般來說你要提交你的數據庫獲取數據時(至少我剛給的例子)

+1

是的,它使用數據庫的工作,但我正在處理文件。而已!這是工作 – aaron0207