andersao / andersao/l5-repository

Call to undefined method Illuminate\Database\Query\Builder::all()

Aperta
#721 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
PHP
Stelle
4.2k
Fork
880
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

I have the following code at my controller:


```
public function popularindex(Request $request)
{
try {
$this->restaurantRepository->pushCriteria(new PopularCriteria($request));
$this->restaurantRepository->pushCriteria(new LimitOffsetCriteria($request));

$restaurants = $this->restaurantRepository->all();
} catch (RepositoryException $e) {
return $this->sendError($e->getMessage());
}

return $this->sendResponse($restaurants->toArray(), 'Popular Restaurants retrieved successfully');
}
```

And my PopularCriteria have:

```
public function apply($model, RepositoryInterface $repository)
{
$subquery = DB::table(DB::raw('restaurants r'))
->select(DB::raw("count(*) as review_count, r.*, round(avg(rr.rate), 1) as rate"))
->join(DB::Raw('restaurant_reviews rr'), DB::Raw('rr.restaurant_id'), '=', DB::Raw('r.id'))
->groupBy(DB::Raw('r.id'))
->orderBy('review_count', 'desc')
->limit(3);

$query = DB::table(DB::raw("({$subquery->toSql()}) r"))
->select(DB::raw('r.*'))
->orderBy('rate', 'desc');

return $query;
}
```
The criteria itself is quite complex, as it handle a subquery. But the thing is, i read somewhere that we should not use `all()` when dealing with constraints, but `get()` instead. So how should I deal with this situation, to use this Criteria, and be able to use `$restaurants = $this->restaurantRepository->all();` , so that I can get for example other atributes that is set at my Model (Restaurant), like "[Appends](https://laravel.com/docs/5.8/eloquent-serialization#appending-values-to-json)", that is processed at this point, when finding the results. When I try to run the above, I get the following error:

```
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::all()
```

I have others different criterias set at this way, but not so complex ones like this one.

Having a deep look at vendor files, I see that there is an if clause that determines when should be `all()` or `get()`, but I'm not how I could work with it:

```
public function all($columns = ['*'])
{
$this->applyCriteria();
$this->applyScope();

if ($this->model instanceof Builder) {
$results = $this->model->get($columns);
} else {
$results = $this->model->all($columns);
}

$this->resetModel();
$this->resetScope();

return $this->parserResult($results);
}
```

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.