record of custom page to widget

How to access $record in widget while widget is registered in custom pages
13 Replies
ba_mbi_07
ba_mbi_07OP2y ago
class CategoryHandler extends BaseWidget
{
public ?Model $record = null;

protected function getTableQuery(): Builder
{
dd($this->record);
return PageHandler::query()
->where('type', 'subCat')
->where('parent_id', '=', $this->record?->category_id);
}
class CategoryHandler extends BaseWidget
{
public ?Model $record = null;

protected function getTableQuery(): Builder
{
dd($this->record);
return PageHandler::query()
->where('type', 'subCat')
->where('parent_id', '=', $this->record?->category_id);
}
this is my widget class
Vp
Vp2y ago
Edit blade file like this <x-filament::page :widget-data="['key' => $value]">
ba_mbi_07
ba_mbi_07OP2y ago
getting error for this Undefined variable $record this is my blade file <x-filament::page :widget-data="['record' => $record]"> </x-filament::page>
Vp
Vp2y ago
what is $record contain from custom page, not widget?
ba_mbi_07
ba_mbi_07OP2y ago
i am getting record if i am registering widget in editpage ...if i registered my widget in custom page i am getting record null this is my custom class
class ManagePageAndLink extends Page
{
protected static string $resource = CategoryPageLogHandlerResource::class;

protected static string $view = 'filament.resources.category-page-log-handler-resource.pages.manage-page-and-link';

protected function getHeaderWidgets(): array
{
return [
CategoryHandler::class,
];
}
}
class ManagePageAndLink extends Page
{
protected static string $resource = CategoryPageLogHandlerResource::class;

protected static string $view = 'filament.resources.category-page-log-handler-resource.pages.manage-page-and-link';

protected function getHeaderWidgets(): array
{
return [
CategoryHandler::class,
];
}
}
Vp
Vp2y ago
Yes, edit and view page contain data, but you are in custom page.. unless you define what you need in $variable you cannot get.. create $record in custom page and you can see what is happening in the blade file
ba_mbi_07
ba_mbi_07OP2y ago
how to declare $record in custom page
Vp
Vp2y ago
public $record;

public function mount(){
$this->record = User::first();
}
public $record;

public function mount(){
$this->record = User::first();
}
you can have an idea from here
ba_mbi_07
ba_mbi_07OP2y ago
thanks i got record from url is there any better way
public $record;

public function mount($record): void
{
$url = URL::current();

$parts = explode('/category-page-handler/', $url);

if (count($parts) >= 2) {
$numberPart = $parts[1];
$number = strtok($numberPart, '/');
$this->record = WpTerm::where('term_id', $number)->first();
}
}
public $record;

public function mount($record): void
{
$url = URL::current();

$parts = explode('/category-page-handler/', $url);

if (count($parts) >= 2) {
$numberPart = $parts[1];
$number = strtok($numberPart, '/');
$this->record = WpTerm::where('term_id', $number)->first();
}
}
Vp
Vp2y ago
First explain what you want to pass in widget.. The ID or whole Model? $this->record = WpTerm::whereTermId($record)->first(); check what is $record inside mount()
ba_mbi_07
ba_mbi_07OP2y ago
http://127.0.0.1:8000/admin/category-page-handler/6336/manage this is my url i am passing $record as 6336 from current url
Vp
Vp2y ago
Do like what I written there, $record contain 6336 inside mount().. like this
ba_mbi_07
ba_mbi_07OP2y ago
yes
Want results from more Discord servers?
Add your server