How to prevent passing invalid record ID to custom resource page URL in Filament?

Hi everyone, I have a custom page in Filament that lists exams and quizzes, and a custom resource page for quizzes where the user can take the quiz. However, whenever I pass a $record to the URL of the custom resource page that does not exist in the database, the page still loads, which should not be the case. Can someone please advise on how I can ensure that the custom resource page only loads if the $record exists in the database? Thank you.
5 Replies
Tin
Tin2y ago
If you are redirecting from Resource to a Page. In the page mount() method you can do Model::findOrFail($recordId)
ianclemence
ianclemenceOP2y ago
Where should i define the $recordId variable?
Tin
Tin2y ago
I think it should be passed in mount. If not, maybe you can get it from request() since it's in the url
Dennis Koch
Dennis Koch2y ago
Probably something like this:
public function mount($record)
{
$this->record = Model::findOrFail($record);
}
public function mount($record)
{
$this->record = Model::findOrFail($record);
}
ianclemence
ianclemenceOP2y ago
Thank you both. It works now. Really appreciate
Want results from more Discord servers?
Add your server