Active actions

You're browsing the documentation for an old version of MoonShine. Consider upgrading your project to MoonShine 2.x.

Quite often you need to create a resource with the disabled delete or add or edit section. And we are not talking about authorization here, but about the global exclusion of these sections. This could be done very easy by using the activeActions property in the resource

namespace MoonShine\Resources;
 
class PostResource extends Resource
{
public static string $model = App\Models\Post::class;
 
public static string $title = 'Articles';
 
public static array $activeActions = ['create', 'show', 'edit', 'delete'];
//...
}

Just eliminate the needless one

public static array $activeActions = ['create'];

You can also use the getActiveActions() method and set your own logic for the available sections

namespace MoonShine\Resources;
 
class PostResource extends Resource
{
public static string $model = App\Models\Post::class;
 
public static string $title = 'Articles';
 
public static array $activeActions = ['create', 'show', 'edit'];
 
//...
 
public function getActiveActions(): array
{
if (auth()->id() === $this->getItem()?->author_id) {
return array_merge(static::$activeActions, ['delete']);
}
 
return static::$activeActions;
}
}