Query

You're browsing the documentation for an old version of MoonShine. Consider upgrading your project to MoonShine 2.x.

# Filter

Quite often you have to filter the output with a list of records from the very start.

In general, you can easily override the query builder in the resource.

namespace MoonShine\Resources;
 
use Illuminate\Contracts\Database\Eloquent\Builder;
 
class PostResource extends Resource
{
//...
public function query(): Builder
{
return parent::query()
->where('active', true);
}
//...
}

# Order

By overriding the performOrder() method, you can customize the sorting of elements.

namespace MoonShine\Resources;
 
use Illuminate\Contracts\Database\Eloquent\Builder;
 
class PostResource extends Resource
{
//...
public function performOrder(Builder $query, string $column, string $direction): Builder
{
return $query->orderBy(
$column,
$direction
);
}
//...
}

# Scopes

Laravel has a handy scopes functionality that could be used within the MoonShine admin panel.

At first, you need to create the scopes required.

php artisan make:scope ActiveUserScope
namespace MoonShine\Resources;
 
use App\Models\Scopes\ActiveUserScope;
 
class PostResource extends Resource
{
//...
public function scopes(): array
{
return [
new ActiveUserScope()
];
}
//...
}