namespace App\MoonShine\Resources;
use App\Models\Post;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
protected string $sortColumn = ''; // Default sort field
protected string $sortDirection = 'DESC'; // Default sort type
protected int $itemsPerPage = 25; // Number of elements per page
//...
}
namespaceApp\MoonShine\Resources;
useApp\Models\Post;
useMoonShine\Resources\ModelResource;
classPostResourceextendsModelResource
{
protectedstring $sortColumn =''; // Default sort field
protectedstring $sortDirection ='DESC'; // Default sort type
protectedint $itemsPerPage =25; // Number of elements per page
//...
}
namespace App\MoonShine\Resources;
use App\Models\Post;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
protected string $sortColumn = ''; // Default sort field
protected string $sortDirection = 'DESC'; // Default sort type
protected int $itemsPerPage = 25; // Number of elements per page
//...
}
Through model resources, it is possible to customize the data table tr and td.
To do this, you must use the appropriate trAttributes() and tdAttributes() methods, which need to pass a closure that returns attributes for the table component.
namespace App\MoonShine\Resources;
use App\Models\Post;
use Closure;
use Illuminate\View\ComponentAttributeBag;
use MoonShine\Fields\Text;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
protected string $model = Post::class;
protected string $title = 'Posts';
//...
public function trAttributes(): Closure
{
return function (
Model $item,
int $row,
ComponentAttributeBag $attr
): ComponentAttributeBag {
if ($item->id === 1 | $row === 2) {
$attr->setAttributes([
'class' => 'bgc-green'
]);
}
return $attr;
};
}
public function tdAttributes(): Closure
{
return function (
Model $item,
int $row,
int $cell,
ComponentAttributeBag $attr = null
): ComponentAttributeBag {
if ($cell === 6) {
$attr->setAttributes([
'class' => 'bgc-red'
]);
}
return $attr;
};
}
//...
}
namespaceApp\MoonShine\Resources;
useApp\Models\Post;
useClosure;
useIlluminate\View\ComponentAttributeBag;
useMoonShine\Fields\Text;
useMoonShine\Resources\ModelResource;
classPostResourceextendsModelResource
{
protectedstring $model =Post::class;
protectedstring $title ='Posts';
//...
publicfunctiontrAttributes():Closure
{
returnfunction (
Model $item,
int $row,
ComponentAttributeBag $attr
):ComponentAttributeBag {
if ($item->id ===1| $row ===2) {
$attr->setAttributes([
'class'=>'bgc-green'
]);
}
return $attr;
};
}
publicfunctiontdAttributes():Closure
{
returnfunction (
Model $item,
int $row,
int $cell,
ComponentAttributeBag $attr =null
):ComponentAttributeBag {
if ($cell ===6) {
$attr->setAttributes([
'class'=>'bgc-red'
]);
}
return $attr;
};
}
//...
}
namespace App\MoonShine\Resources;
use App\Models\Post;
use Closure;
use Illuminate\View\ComponentAttributeBag;
use MoonShine\Fields\Text;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
protected string $model = Post::class;
protected string $title = 'Posts';
//...
public function trAttributes(): Closure
{
return function (
Model $item,
int $row,
ComponentAttributeBag $attr
): ComponentAttributeBag {
if ($item->id === 1 | $row === 2) {
$attr->setAttributes([
'class' => 'bgc-green'
]);
}
return $attr;
};
}
public function tdAttributes(): Closure
{
return function (
Model $item,
int $row,
int $cell,
ComponentAttributeBag $attr = null
): ComponentAttributeBag {
if ($cell === 6) {
$attr->setAttributes([
'class' => 'bgc-red'
]);
}
return $attr;
};
}
//...
}
If you don't plan to display the total number of pages, use Simple Pagination. This will avoid additional queries for the total number of records in the database.
namespace App\MoonShine\Resources;
use App\Models\Post;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
protected string $model = Post::class;
protected string $title = 'Posts';
protected bool $simplePaginate = true;
// ...
}
namespaceApp\MoonShine\Resources;
useApp\Models\Post;
useMoonShine\Resources\ModelResource;
classPostResourceextendsModelResource
{
protectedstring $model =Post::class;
protectedstring $title ='Posts';
protectedbool $simplePaginate =true;
// ...
}
namespace App\MoonShine\Resources;
use App\Models\Post;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
protected string $model = Post::class;
protected string $title = 'Posts';
protected bool $simplePaginate = true;
// ...
}
You can let users decide which columns to display in the table, saving the choice. To do this, you need to set the resource parameter $columnSelection.
namespace App\MoonShine\Resources;
use App\Models\Post;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
protected string $model = Post::class;
protected string $title = 'Posts';
protected bool $columnSelection = true;
//...
}
namespace App\MoonShine\Resources;
use App\Models\Post;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
protected string $model = Post::class;
protected string $title = 'Posts';
protected bool $columnSelection = true;
//...
}
namespace App\MoonShine\Resources;
use App\Models\Post;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
protected string $model = Post::class;
protected string $title = 'Posts';
protected bool $columnSelection = true;
//...
}
If you need to exclude fields from selection, use the columnSelection() method.
public function columnSelection(bool $active = true)
publicfunctioncolumnSelection(bool $active =true)
public function columnSelection(bool $active = true)
namespace App\MoonShine\Resources;
use App\Models\Post;
use MoonShine\Fields\ID;
use MoonShine\Fields\Text;
use MoonShine\Fields\Textarea;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
protected string $model = Post::class;
protected string $title = 'Posts';
protected bool $columnSelection = true;
//...
public function fields(): array
{
return [
ID::make()
->columnSelection(false),
Text::make('Title'),
Textarea::make('Body'),
];
}
//...
}
namespaceApp\MoonShine\Resources;
useApp\Models\Post;
useMoonShine\Fields\ID;
useMoonShine\Fields\Text;
useMoonShine\Fields\Textarea;
useMoonShine\Resources\ModelResource;
classPostResourceextendsModelResource
{
protectedstring $model =Post::class;
protectedstring $title ='Posts';
protectedbool $columnSelection =true;
//...
publicfunctionfields():array
{
return [
ID::make()
->columnSelection(false),
Text::make('Title'),
Textarea::make('Body'),
];
}
//...
}
namespace App\MoonShine\Resources;
use App\Models\Post;
use MoonShine\Fields\ID;
use MoonShine\Fields\Text;
use MoonShine\Fields\Textarea;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
protected string $model = Post::class;
protected string $title = 'Posts';
protected bool $columnSelection = true;
//...
public function fields(): array
{
return [
ID::make()
->columnSelection(false),
Text::make('Title'),
Textarea::make('Body'),
];
}
//...
}
You can replace thead or tbody or tfoot, and also add elements to the table in tbody before and after the first row.
thead()
namespace App\MoonShine\Resources;
use MoonShine\Fields\Fields;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
public function thead(): ?Closure
{
return static fn(Fields $headFields): string => '
namespace App\MoonShine\Resources;
use MoonShine\Fields\Fields;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
public function thead(): ?Closure
{
return static fn(Fields $headFields): string => '<tr><th>Title</th></tr>';
}
}
tbody()
namespace App\MoonShine\Resources;
use Illuminate\Support\Collection;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
public function tbody(): ?Closure
{
return static fn(Collection $rows): string => '
namespace App\MoonShine\Resources;
use Illuminate\Support\Collection;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
public function tbody(): ?Closure
{
return static fn(Collection $rows): string => '<tr><td>Content</td></tr>';
}
}
tfoot()
namespace App\MoonShine\Resources;
use MoonShine\ActionButtons\ActionButtons;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
public function tfoot(): ?Closure
{
return static fn(ActionButtons $bulkButtons): string => '
namespace App\MoonShine\Resources;
use MoonShine\ActionButtons\ActionButtons;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
public function tfoot(): ?Closure
{
return static fn(ActionButtons $bulkButtons): string => '<tr><td>Footer</td></tr>';
}
}
tbodyBefore()
namespace App\MoonShine\Resources;
use Illuminate\Support\Collection;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
public function tbodyBefore(): ?Closure
{
return static fn(Collection $rows): string => '
namespace App\MoonShine\Resources;
use Illuminate\Support\Collection;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
public function tbodyBefore(): ?Closure
{
return static fn(Collection $rows): string => '<tr><td>Before</td></tr>';
}
}
tbodyAfter()
namespace App\MoonShine\Resources;
use Illuminate\Support\Collection;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
public function tbodyAfter(): ?Closure
{
return static fn(Collection $rows): string => '