Separate methods

Hi, guys, is it possible to separate infolist and table to View and List page? I have really long resources will all of these in one place.
10 Replies
Patrick Boivin
Patrick Boivin17mo ago
Can you share a screenshot, I'm not sure I can visualize it...
Trauma Zombie
Trauma ZombieOP17mo ago
I mean, if I can move infolist method to ViewRecord class and table method to ListRecord class.
Patrick Boivin
Patrick Boivin17mo ago
Sure, you can move the static methods wherever you want... It could be even better to move them to a separate class, but you still need a call in the resource:
class PostResource extends Resource
{
// ...

public static function form(Form $form): Form
{
return $form->schema(PostForm::schema());
}
}
class PostResource extends Resource
{
// ...

public static function form(Form $form): Form
{
return $form->schema(PostForm::schema());
}
}
class PostForm
{
public static function schema(): array
{
return [
// ...
];
}
}
class PostForm
{
public static function schema(): array
{
return [
// ...
];
}
}
Something like that... make it nice 😄
Dennis Koch
Dennis Koch17mo ago
With v3 you can actually just use the Resource syntax on the pages themselves.
Patrick Boivin
Patrick Boivin17mo ago
That's awesome And they are instance methods (not static), super useful:
public function form(Form $form): Form {}

public function table(Table $table): Table {}
public function form(Form $form): Form {}

public function table(Table $table): Table {}
Trauma Zombie
Trauma ZombieOP17mo ago
I am getting this error: Infolist has no [record()] or [state()] set. I use it in some TextEntry components.
Patrick Boivin
Patrick Boivin17mo ago
We'll need to see the code if possible
Trauma Zombie
Trauma ZombieOP17mo ago
<?php

namespace App\Filament\Resources\CartResource\Pages;

use App\Enums\CartStatus;
use App\Filament\Resources\{CartResource, CustomerResource, OrderResource};
use App\Models\Cart;
use Filament\Infolists\Infolist;
use Filament\Resources\Pages\ViewRecord;
use Filament\{Actions, Infolists};

class ViewCart extends ViewRecord
{
protected static string $resource = CartResource::class;

protected function getHeaderActions(): array
{
return [
Actions\EditAction::make(),
Actions\DeleteAction::make(),
];
}

public function infolist(Infolist $infolist): Infolist
{
return $infolist
->schema([
Infolists\Components\TextEntry::make('customer.full_name')
->label(__('Customer'))
->url(fn (Cart $record) => $record->customer ? CustomerResource::getUrl('edit', ['record' => $record->customer->id]) : null)
->default(__('Guest')),

Infolists\Components\TextEntry::make('order.reference')
->label(__('Order'))
->url(fn (Cart $record) => $record->order ? OrderResource::getUrl('edit', ['record' => $record->order->id]) : null)
->default(__('None')),
])
->columns(3);
}
}
<?php

namespace App\Filament\Resources\CartResource\Pages;

use App\Enums\CartStatus;
use App\Filament\Resources\{CartResource, CustomerResource, OrderResource};
use App\Models\Cart;
use Filament\Infolists\Infolist;
use Filament\Resources\Pages\ViewRecord;
use Filament\{Actions, Infolists};

class ViewCart extends ViewRecord
{
protected static string $resource = CartResource::class;

protected function getHeaderActions(): array
{
return [
Actions\EditAction::make(),
Actions\DeleteAction::make(),
];
}

public function infolist(Infolist $infolist): Infolist
{
return $infolist
->schema([
Infolists\Components\TextEntry::make('customer.full_name')
->label(__('Customer'))
->url(fn (Cart $record) => $record->customer ? CustomerResource::getUrl('edit', ['record' => $record->customer->id]) : null)
->default(__('Guest')),

Infolists\Components\TextEntry::make('order.reference')
->label(__('Order'))
->url(fn (Cart $record) => $record->order ? OrderResource::getUrl('edit', ['record' => $record->order->id]) : null)
->default(__('None')),
])
->columns(3);
}
}
Patrick Boivin
Patrick Boivin17mo ago
I think you're missing some of the setup that happens in the parent, try:
return parent::infolist($infolist)
->schema([
// ...
])
return parent::infolist($infolist)
->schema([
// ...
])
Trauma Zombie
Trauma ZombieOP17mo ago
Yeah, it works just fine. Thank you @pboivin. I like it more like this.
Want results from more Discord servers?
Add your server