It is often necessary to initially modify all resource queries to the database.
You can easily override the QueryBuilder
in the resource.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:4]
namespace App\MoonShine\Resources;
use Illuminate\Contracts\Database\Eloquent\Builder;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function modifyQueryBuilder(Builder $builder): Builder
{
return $builder->where('active', true);
}
}
namespaces
namespace App\MoonShine\Resources;
use Illuminate\Contracts\Database\Eloquent\Builder;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function modifyQueryBuilder(Builder $builder): Builder
{
return $builder->where('active', true);
}
}
namespace App\MoonShine\Resources;
use Illuminate\Contracts\Database\Eloquent\Builder;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function modifyQueryBuilder(Builder $builder): Builder
{
return $builder->where('active', true);
}
}
If you need to completely override the Builder
, you can override the resource method newQuery()
.
The modifyItemQueryBuilder()
method is used if you need to modify the query for retrieving a record from the database.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:4]
namespace App\MoonShine\Resources;
use Illuminate\Contracts\Database\Eloquent\Builder;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function modifyItemQueryBuilder(Builder $builder): Builder
{
return $builder->withTrashed();
}
}
namespaces
namespace App\MoonShine\Resources;
use Illuminate\Contracts\Database\Eloquent\Builder;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function modifyItemQueryBuilder(Builder $builder): Builder
{
return $builder->withTrashed();
}
}
namespace App\MoonShine\Resources;
use Illuminate\Contracts\Database\Eloquent\Builder;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function modifyItemQueryBuilder(Builder $builder): Builder
{
return $builder->withTrashed();
}
}
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:3]
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
protected array $with = ['user', 'categories'];
// ...
}
namespaces
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
protected array $with = ['user', 'categories'];
// ...
}
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
protected array $with = ['user', 'categories'];
// ...
}
If you need to completely override the Builder
, you can override the resource method findItem()
.
The searchQuery()
method allows you to modify the query when searching for records.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:3]
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function searchQuery(string $terms): void
{
return parent::searchQuery($terms)->withTrashed();
}
}
namespaces
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function searchQuery(string $terms): void
{
return parent::searchQuery($terms)->withTrashed();
}
}
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function searchQuery(string $terms): void
{
return parent::searchQuery($terms)->withTrashed();
}
}
You can also completely override the search logic.
protected function resolveSearch(string $terms, ?iterable $fullTextColumns = null): static
{
// Your logic
return $this;
}
protected function resolveSearch(string $terms, ?iterable $fullTextColumns = null): static
{
// Your logic
return $this;
}
protected function resolveSearch(string $terms, ?iterable $fullTextColumns = null): static
{
// Your logic
return $this;
}
By overriding the resolveOrder()
method, you can customize the sorting of records.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:3]
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function resolveOrder(string $column, string $direction, ?Closure $callback): static
{
if ($callback instanceof Closure) {
$callback($this->newQuery(), $column, $direction);
} else {
$this->newQuery()->orderBy($column, $direction);
}
return $this;
}
}
namespaces
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function resolveOrder(string $column, string $direction, ?Closure $callback): static
{
if ($callback instanceof Closure) {
$callback($this->newQuery(), $column, $direction);
} else {
$this->newQuery()->orderBy($column, $direction);
}
return $this;
}
}
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function resolveOrder(string $column, string $direction, ?Closure $callback): static
{
if ($callback instanceof Closure) {
$callback($this->newQuery(), $column, $direction);
} else {
$this->newQuery()->orderBy($column, $direction);
}
return $this;
}
}