Passing the record in the helperText method

is it possible to pass the $record in the helperText() function? tried doing it in the 'regular' way but it seems like $record is always null and im not sure why it behaves like this.
5 Replies
prowler
prowlerOP5mo ago
i managed to solve it with some workaround using the Get $get approach, but still, i wonder why some methods are able to get the $record and some not.
LeandroFerreira
LeandroFerreira5mo ago
Could you share the code?
prowler
prowlerOP5mo ago
Forms\Components\Select::make('content_type_variation_id')
->helperText(function (Forms\Get $get) {
if ($get('content_type_variation_id')) {
$contentTypeVariationId = $get('content_type_variation_id');
$sampleArticles = Article::whereHas('brief', function ($query) use ($contentTypeVariationId) {
$query->where('content_type_variation_id', $contentTypeVariationId);
})->isSample()->get();
if(count($sampleArticles) > 0) {
$urls = $sampleArticles->map(function ($article, $key) {
return '<a style="color: #0044cc;" class="font-semibold" target="_blank" href="' . route('article.preview', $article->slug) . '">Sample #' . $key+1 . '</a>';
});
return new HtmlString($urls->implode(' / '));
}
else {
return 'No samples available for the selected content type variation.';
}
}
return 'No samples available for the selected content type variation.';
})
Forms\Components\Select::make('content_type_variation_id')
->helperText(function (Forms\Get $get) {
if ($get('content_type_variation_id')) {
$contentTypeVariationId = $get('content_type_variation_id');
$sampleArticles = Article::whereHas('brief', function ($query) use ($contentTypeVariationId) {
$query->where('content_type_variation_id', $contentTypeVariationId);
})->isSample()->get();
if(count($sampleArticles) > 0) {
$urls = $sampleArticles->map(function ($article, $key) {
return '<a style="color: #0044cc;" class="font-semibold" target="_blank" href="' . route('article.preview', $article->slug) . '">Sample #' . $key+1 . '</a>';
});
return new HtmlString($urls->implode(' / '));
}
else {
return 'No samples available for the selected content type variation.';
}
}
return 'No samples available for the selected content type variation.';
})
Basically, it works but if i could get the $record in the closure - i'd have much less code as i could directly check for $record's relationship
LeandroFerreira
LeandroFerreira5mo ago
$record is available in the Edit/View page
prowler
prowlerOP5mo ago
yeah, i made some confusion there thinking that the $record in this case is the record chosen from the Select component (content_type_variation_id). My bad. Thanks for clarifying it
Want results from more Discord servers?
Add your server