search - How to skip a row with file exists condition in laravel -


this search query based on many input fields, i'm doing if statements inside query based on inputs, example :

$query = model::all();  if($field = input::get('field'))         $query->where('column_name', $field); 

but want condition skip row if there no image name of row id, :

if( file_exists('/img/'.$this_query->id) )         $query->skip(); 

option 1: make array of filenames, utilize wherein query builder method, checks occurrence of column value in array.

$query = model::query();  $filenames = scandir('/img');  // following excludes id's not in $filenames array $query = $query->wherein('id', $filenames); 

option 2: run query , filter resulting collection in laravel.

$query = model::query(); // ... build query  // following gets query results , filters them out $collection = $query->get()->filter( function($item) {     return file_exists('/img/'.$item->id); } 

rationale: database cannot check filesystem during it's processing; either need (option 1) provide list check, or (option 2) checking after get() results query.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -