for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Spatie\QueryBuilder\Concerns;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Spatie\QueryBuilder\Exceptions\InvalidAppendQuery;
trait AppendsAttributesToResults
{
/** @var \Illuminate\Support\Collection */
protected $allowedAppends;
public function allowedAppends($appends): self
$appends = is_array($appends) ? $appends : func_get_args();
$this->allowedAppends = collect($appends);
$this->ensureAllAppendsExist();
return $this;
}
protected function addAppendsToResults(Collection $results)
if (! $this->ensureAllAppendsExist()) {
return $results;
return $results->each(function (Model $result) {
return $result->append($this->request->appends()->toArray());
request
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
});
protected function ensureAllAppendsExist(): bool
$appends = $this->request->appends();
$diff = $appends->diff($this->allowedAppends);
if ($diff->count()) {
if ($this->throwInvalidQueryExceptions) {
throwInvalidQueryExceptions
throw InvalidAppendQuery::appendsNotAllowed($diff, $this->allowedAppends);
} else {
return false;
return true;
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: