examples/ProjectRepository.php
protected function afterQuery($result)
    {
        // $result can be a single model, a paginator or a collection. We can
        // standardize the variable in this way to be able to use the same code
        // on all three types.
        if ($result instanceof \Illuminate\Database\Eloquent\Model) {
            // we might just do `return;` here if we didn't want our logic
            // applied to a single model.
            $projects = $result->newCollection([$result]);
        } else if ($result instanceof \Illuminate\Pagination\Paginator) {
            $projects = $result->getCollection();
        }

        // let's say we want to add some data onto the models after they've
        // been retrieved from the database. we want to add the count of a
        // relation, but don't want to mess with sub-selects and don't want to
        // waste memory by eager loading the relation, nor do we want to run
        // one query for each model.

        // build the query that gets the relationship count. some of this logic
        // could be moved into model scopes if you want. this is a rather
        // complex query so if you don't understand what is going on here,
        // don't worry too much. basically just getting the number of relations
        // attached to each project.
        $results = $this->model->relation()
            ->getRelated()->newQuery()
            ->whereIn('parent_id', $projects->lists('id'))
            ->selectRaw('parent_id, count(*) as relation_count')
            ->groupBy('parent_id')
            ->lists('relation_count', 'parent_id');

        // now add the attribute to each model.
        $projects->each(function($project) {
            $project->relation_count = $results[$project->id];
        });
    }
protected function afterQuery($result)
    {
        // $result can be a single model, a paginator or a collection. We can
        // standardize the variable in this way to be able to use the same code
        // on all three types.
        if ($result instanceof \Illuminate\Database\Eloquent\Model) {
            // we might just do `return;` here if we didn't want our logic
            // applied to a single model.
            $projects = $result->newCollection([$result]);
        } else if ($result instanceof \Illuminate\Pagination\Paginator) {
            $projects = $result->getCollection();
        }

        // let's say we want to add some data onto the models after they've
        // been retrieved from the database. we want to add the count of a
        // relation, but don't want to mess with sub-selects and don't want to
        // waste memory by eager loading the relation, nor do we want to run
        // one query for each model.

        // build the query that gets the relationship count. some of this logic
        // could be moved into model scopes if you want. this is a rather
        // complex query so if you don't understand what is going on here,
        // don't worry too much. basically just getting the number of relations
        // attached to each project.
        $results = $this->model->relation()
            ->getRelated()->newQuery()
            ->whereIn('parent_id', $projects->lists('id'))
            ->selectRaw('parent_id, count(*) as relation_count')
            ->groupBy('parent_id')
            ->lists('relation_count', 'parent_id');

        // now add the attribute to each model.
        $projects->each(function($project) {
            $project->relation_count = $results[$project->id];
        });
    }
This method is called after fetchMany and fetchSingle. It is most useful
to append attributes onto more than one model for stuff that can't easily
be done via the query builder. Sometimes, for example, a subselect or
join or groupby can mess with the paginator.

Changed from prepareCollection/preparePaginator in 0.5