F
Filament16mo ago
Kleis

How to access current model in custom view page

I made a livewire component and included it in a view page, but how can I access the current model in order to show the related data ? Basically I want to make table of the relation with links to edit that relation, but to keep the edit page clean I dont want to include a relation manager there.
class ViewApartment extends Component implements HasForms, HasTable
{
use InteractsWithTable;
use InteractsWithForms;

public function table(Table $table): Table
{
return $table
->query(function (\App\Models\Apartment $record) { \Log::debug($record); return \App\Models\Ownership::where("apartment_name", $record->name); })
->columns([
TextColumn::make('name'),
])
->filters([])
->actions([])
->bulkActions([]);
}

public function render()
{
return view('livewire.view-apartment');
}
}
class ViewApartment extends Component implements HasForms, HasTable
{
use InteractsWithTable;
use InteractsWithForms;

public function table(Table $table): Table
{
return $table
->query(function (\App\Models\Apartment $record) { \Log::debug($record); return \App\Models\Ownership::where("apartment_name", $record->name); })
->columns([
TextColumn::make('name'),
])
->filters([])
->actions([])
->bulkActions([]);
}

public function render()
{
return view('livewire.view-apartment');
}
}
3 Replies
LeandroFerreira
LeandroFerreira16mo ago
Livewire
Rendering Components | Livewire
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
Wannes
Wannes16mo ago
Define
public ?Model $record = null;
public ?Model $record = null;
. You can access it with $this->recordd than.
Kleis
KleisOP16mo ago
Just tried, but its null :/ I am guessing it gets lost somewhere along the way. ApartmentResource has a view route : 'view' => Pages\ViewApartment::route('/{record}'), That route includes the component : @livewire('View-apartment') That component shows the table : {{ $this->table }} Which is defied in the livewire component 🫣 So I am thinking that the first view that calls the @livewire needs to forward the param ? It works \o/ thanks both. @livewire('View-apartment', ['record'=>$record]) public ?\App\Models\Apartment $record = null;
Want results from more Discord servers?
Add your server