F
Filament13mo ago
Chanda

Listing records from a custom page:

Hello I want to create a custom page for listing records only. I don't want a full resource but I only want a page to list records which will be linked to a certain resource which has already benn created, so I proceeded to create my bew resource as per docs as: php artisan make:filament-page ActiveLoans --resource=LoanResource --type=custom Upon creating my custom page I registered my custom page route in the `LoanResource file class under getPages as shown:
public static function getPages(): array
{
return [
'active' => Pages\ActiveLoans::route('/active'),
'index' => Pages\ListLoans::route('/'),
'create' => Pages\CreateLoan::route('/create'),
'view' => Pages\ViewLoan::route('/{record}'),
'edit' => Pages\EditLoan::route('/{record}/edit'),

];
}
public static function getPages(): array
{
return [
'active' => Pages\ActiveLoans::route('/active'),
'index' => Pages\ListLoans::route('/'),
'create' => Pages\CreateLoan::route('/create'),
'view' => Pages\ViewLoan::route('/{record}'),
'edit' => Pages\EditLoan::route('/{record}/edit'),

];
}
I then went to my newly created custom page class component and added the following files:
<?php

namespace App\Filament\Resources\LoanResource\Pages;
use Illuminate\Support\Collection;
use App\Filament\Resources\LoanResource;
use App\Models\Loan;
use Filament\Resources\Pages\Page;

class ActiveLoans extends Page
{
protected static string $resource = LoanResource::class;

protected static string $view = 'filament.resources.loan-resource.pages.active-loans';


public function getViewData(): array {
$data = Loan::get();

if ($data instanceof Collection) {
return $data->toArray();
}


}
}
<?php

namespace App\Filament\Resources\LoanResource\Pages;
use Illuminate\Support\Collection;
use App\Filament\Resources\LoanResource;
use App\Models\Loan;
use Filament\Resources\Pages\Page;

class ActiveLoans extends Page
{
protected static string $resource = LoanResource::class;

protected static string $view = 'filament.resources.loan-resource.pages.active-loans';


public function getViewData(): array {
$data = Loan::get();

if ($data instanceof Collection) {
return $data->toArray();
}


}
}
The corresponding laravel blade for the custom class looks like this:
<x-filament-panels::page>
{{ $this->data }}
</x-filament-panels::page>
<x-filament-panels::page>
{{ $this->data }}
</x-filament-panels::page>
However each time I access my route am getting the following exception : Property [$data] not found on component: [app.filament.resources.loan-resource.pages.active-loans] What am I missing?
5 Replies
Bogardo
Bogardo13mo ago
Instead of using $this->data, shouldn’t you be able to just use {{ $data }} because it’s already passed to the view?
Chanda
ChandaOP13mo ago
@Bogardo tried I don't know why its saying Undefined variable $data
Bogardo
Bogardo13mo ago
Are you a 100% sure that the $data variable in your getViewData method has the correct type?
Chanda
ChandaOP13mo ago
@Bogardo you are right I wasn't passing the view to the blade file: this is how It was supposed to be:
<?php
namespace App\Filament\Resources\LoanResource\Pages;

use App\Filament\Resources\LoanResource;
use App\Models\Loan;
use Filament\Resources\Pages\Page;

class ActiveLoans extends Page
{
protected static string $resource = LoanResource::class;

protected static string $view = 'filament.resources.loan-resource.pages.active-loans';

public function getViewData(): array
{
$data = Loan::get()->toArray();
return ['data' => $data];
}
}
<?php
namespace App\Filament\Resources\LoanResource\Pages;

use App\Filament\Resources\LoanResource;
use App\Models\Loan;
use Filament\Resources\Pages\Page;

class ActiveLoans extends Page
{
protected static string $resource = LoanResource::class;

protected static string $view = 'filament.resources.loan-resource.pages.active-loans';

public function getViewData(): array
{
$data = Loan::get()->toArray();
return ['data' => $data];
}
}
Bogardo
Bogardo13mo ago
@Chanda Glad it’s fixed!
Want results from more Discord servers?
Add your server