LMongo\Eloquent\Builder::get PHP Method

get() public method

Execute the query as a "select" statement.
public get ( array $columns = [] ) : Collection
$columns array
return Collection
    public function get($columns = array())
    {
        $models = $this->getModels($columns);
        // If we actually found models we will also eager load any relationships that
        // have been specified as needing to be eager loaded, which will solve the
        // n+1 query issue for the developers to avoid running a lot of queries.
        if (count($models) > 0) {
            $models = $this->eagerLoadRelations($models);
        }
        return $this->model->newCollection($models);
    }

Usage Example

Example #1
0
 /**
  * Get the hydrated models without eager loading.
  *
  * @param  array  $columns
  * @return array
  */
 public function getModels($columns = array())
 {
     // First, we will simply get the raw results from the query builders which we
     // can use to populate an array with models. We will pass columns
     // that should be selected as well, which are typically just everything.
     $results = $this->query->get($columns);
     if ($this->total) {
         $this->total = $results->countAll();
     }
     $connection = $this->model->getConnectionName();
     $models = array();
     // Once we have the results, we can spin through them and instantiate a fresh
     // model instance for each records we retrieved from the database. We will
     // also set the proper connection name for the model after we create it.
     foreach ($results as $result) {
         $models[] = $model = $this->model->newFromBuilder($result);
         $model->setConnection($connection);
     }
     return $models;
 }