Skip to content

AbstractRepository->getAllBy more than one 'where' #59

@elvendor

Description

@elvendor

I needed more than one condition in getAllBy method and I made changes like this:

public function getAllBy(array $conditions = array(), array $with = array('translations'), $all = false)
{
  $query = $this->make($with);
  if (! $all) {
    // Take only online and translated items
    $query = $query->whereHasOnlineTranslation();
  }
  if(is_array($conditions) && count($conditions))
  {
    foreach($conditions as $key => $value)
    {
      $query->where($key, $value);
    }
  }
  $models = $query->get();
  return $models;
}

And then in any Controller:

$conditions = array(
  'category_id' => $category->id,
  'type' => $type
);
$data = $this->repository->getAllBy($conditions, $relatedModels, false);

What do you think about this?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions