->action can not be used with ->visible on Resource View Page, why?

I noticed that if you use ->visible on Action it can not be used together with ->action clousure. Why action is never triggered in that case? ->url() works just fine but not action.
19 Replies
Jordy
Jordy5mo ago
yes it can? code?
LeandroFerreira
LeandroFerreira5mo ago
share your code..
aurawindsurfing
aurawindsurfingOP5mo ago
<?php

namespace App\Filament\App\Resources\ClassifiedResource\Pages;

use App\Filament\App\Resources\ClassifiedResource;
use App\Helpers\StripeCheckout;
use Filament\Actions\Action;
use Filament\Resources\Pages\ViewRecord;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Http\Request;

class ViewClassified extends ViewRecord
{
protected static string $resource = ClassifiedResource::class;

protected function getActions(): array
{
return [
Action::make('withraw')
->visible(function (Request $request, $record) {
if (empty($request->token) or $request->token !== $record->token)
{
return false;
}

if ($record->deleted_at)
{
return false;
}

return true;
})
->label(__('classifieds.withdraw'))
->action(function ($record)
{
dd('test');
})
->icon('heroicon-m-trash')
->color('danger'),

Action::make(__('classifieds.pay_and_publish'))
->url(fn($record) => (new StripeCheckout)->payment($record))
->visible(function (Request $request, $record) {
if ($record->paid_at)
{
return false;
}

return true;

})
->icon('heroicon-m-credit-card')
->color('info'),
<?php

namespace App\Filament\App\Resources\ClassifiedResource\Pages;

use App\Filament\App\Resources\ClassifiedResource;
use App\Helpers\StripeCheckout;
use Filament\Actions\Action;
use Filament\Resources\Pages\ViewRecord;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Http\Request;

class ViewClassified extends ViewRecord
{
protected static string $resource = ClassifiedResource::class;

protected function getActions(): array
{
return [
Action::make('withraw')
->visible(function (Request $request, $record) {
if (empty($request->token) or $request->token !== $record->token)
{
return false;
}

if ($record->deleted_at)
{
return false;
}

return true;
})
->label(__('classifieds.withdraw'))
->action(function ($record)
{
dd('test');
})
->icon('heroicon-m-trash')
->color('danger'),

Action::make(__('classifieds.pay_and_publish'))
->url(fn($record) => (new StripeCheckout)->payment($record))
->visible(function (Request $request, $record) {
if ($record->paid_at)
{
return false;
}

return true;

})
->icon('heroicon-m-credit-card')
->color('info'),
the second action works, the first one is the problem. as soon as I remove early returns it works but I need them @Leandro Ferreira any ideas?
LeandroFerreira
LeandroFerreira5mo ago
are they headerActions?
aurawindsurfing
aurawindsurfingOP5mo ago
yes the same result if I use getHeaderActions()
LeandroFerreira
LeandroFerreira5mo ago
what happens if you return only true in the visible method?
aurawindsurfing
aurawindsurfingOP5mo ago
it works as expected if I do that the problem is when I use early return for some reason
LeandroFerreira
LeandroFerreira5mo ago
yep, are you using query params in the page? https://livewire.laravel.com/docs/url#basic-usage
Laravel
URL Query Parameters | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
aurawindsurfing
aurawindsurfingOP5mo ago
yes for the token detection @Leandro Ferreira any ideas what might be causing it?
LeandroFerreira
LeandroFerreira5mo ago
Did you try this?
#[Url]
public $token = '';
#[Url]
public $token = '';
and use $this->token ?
aurawindsurfing
aurawindsurfingOP5mo ago
what I'm doing at the moment is this:
#[Url(history: true)]
public $token = '';
#[Url(history: true)]
public $token = '';
$request->token
$request->token
Should I be using $this->token instead to get actions to work? Let me try.
LeandroFerreira
LeandroFerreira5mo ago
yes
aurawindsurfing
aurawindsurfingOP5mo ago
no it does not work with $this only with $request in my case
LeandroFerreira
LeandroFerreira5mo ago
is there ?token=xxx in the url?
aurawindsurfing
aurawindsurfingOP5mo ago
yes of course this does not work:
Action::make(__('classifieds.withdraw'))
->action(fn() => dd('foobar'))
->visible(function (Request $request, $record) {
if (empty($request->token) or $request->token !== $record->token)
{
return false;
}

if ($record->deleted_at)
{
return false;
}

if ($record->paid_at && $record->receipt_url)
{
return true;
}

return false;

})
->icon('heroicon-m-trash')
->color('danger'),
Action::make(__('classifieds.withdraw'))
->action(fn() => dd('foobar'))
->visible(function (Request $request, $record) {
if (empty($request->token) or $request->token !== $record->token)
{
return false;
}

if ($record->deleted_at)
{
return false;
}

if ($record->paid_at && $record->receipt_url)
{
return true;
}

return false;

})
->icon('heroicon-m-trash')
->color('danger'),
but this works:
Action::make(__('classifieds.withdraw'))
->action(fn() => dd('foobar'))
//->visible(function (Request $request, $record) {
// if (empty($request->token) or $request->token !== $record->token)
// {
// return false;
// }
//
// if ($record->deleted_at)
// {
// return false;
// }
//
// if ($record->paid_at && $record->receipt_url)
// {
// return true;
// }
//
// return false;
//
//})
->icon('heroicon-m-trash')
->color('danger'),
Action::make(__('classifieds.withdraw'))
->action(fn() => dd('foobar'))
//->visible(function (Request $request, $record) {
// if (empty($request->token) or $request->token !== $record->token)
// {
// return false;
// }
//
// if ($record->deleted_at)
// {
// return false;
// }
//
// if ($record->paid_at && $record->receipt_url)
// {
// return true;
// }
//
// return false;
//
//})
->icon('heroicon-m-trash')
->color('danger'),
actually even this works:
Action::make(__('classifieds.withdraw'))
->action(fn() => dd('foobar'))
->visible(function (Request $request, $record) {
return true;
})
->icon('heroicon-m-trash')
->color('danger'),
Action::make(__('classifieds.withdraw'))
->action(fn() => dd('foobar'))
->visible(function (Request $request, $record) {
return true;
})
->icon('heroicon-m-trash')
->color('danger'),
the moment you do early return the action does not kick in at all
LeandroFerreira
LeandroFerreira5mo ago
weird, this should work
->visible(fn ($record): bool => filled($this->token) || filled($record->token) || blank($record->deleted_at))
->visible(fn ($record): bool => filled($this->token) || filled($record->token) || blank($record->deleted_at))
aurawindsurfing
aurawindsurfingOP5mo ago
ok it work with the following:
Action::make(__('classifieds.withdraw'))
->action(fn() => dd('foobar'))
->hidden(fn($record): bool => blank($this->token or $this->token !== $record->token or filled($record->deleted_at)))
->visible(fn ($record): bool => filled($record->paid_at and filled($record->receipt_url)))
->icon('heroicon-m-trash')
->color('danger'),
Action::make(__('classifieds.withdraw'))
->action(fn() => dd('foobar'))
->hidden(fn($record): bool => blank($this->token or $this->token !== $record->token or filled($record->deleted_at)))
->visible(fn ($record): bool => filled($record->paid_at and filled($record->receipt_url)))
->icon('heroicon-m-trash')
->color('danger'),
strange it does not with traditional functions tough
LeandroFerreira
LeandroFerreira5mo ago
weird.. but does it work?
aurawindsurfing
aurawindsurfingOP5mo ago
Looks like it I need to test it further. Ill report back when Im sure it does Thank you for your time!
Want results from more Discord servers?
Add your server