PHPackages                             hosnyadeeb/laravel-model-actions - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Admin Panels](/categories/admin)
4. /
5. hosnyadeeb/laravel-model-actions

ActiveLibrary[Admin Panels](/categories/admin)

hosnyadeeb/laravel-model-actions
================================

A Laravel package for generating action classes for models with CRUD operations

v1.3.2(4mo ago)018MITPHPPHP ^8.1

Since Jan 31Pushed 4mo agoCompare

[ Source](https://github.com/hos1992/laravel-model-actions)[ Packagist](https://packagist.org/packages/hosnyadeeb/laravel-model-actions)[ RSS](/packages/hosnyadeeb-laravel-model-actions/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (6)Versions (19)Used By (0)

Laravel Model Actions
=====================

[](#laravel-model-actions)

A Laravel package for generating action classes for Eloquent models with built-in CRUD operations.

Features
--------

[](#features)

- 🚀 **Artisan Command** - Generate all action classes for a model with a single command
- 📦 **Base Action Classes** - Extensible base classes for Index, Show, Store, Update, and Delete operations
- 🎯 **Static &amp; Instance Execution** - Run actions statically or via helper function
- 🔧 **Customizable Stubs** - Publish and customize action stubs to match your coding style
- ✅ **Confirmation Dialogs** - Warns before overwriting existing actions
- 🎨 **Clean Architecture** - Follows single responsibility principle with dedicated action classes
- 🪝 **Lifecycle Hooks** - Before/after hooks for custom logic injection
- 📦 **Bulk Operations** - BulkDelete and BulkUpdate actions for multiple records
- 🔍 **Query Filters** - Built-in search, sort, and date range filtering
- 🔍 **Query Filters** - Built-in search, sort, and date range filtering

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require hosnyadeeb/laravel-model-actions
```

The package will automatically register its service provider.

Publish Assets (Optional)
-------------------------

[](#publish-assets-optional)

### Publish Configuration

[](#publish-configuration)

```
php artisan vendor:publish --tag=model-actions-config
```

### Publish Stubs for Customization

[](#publish-stubs-for-customization)

This allows you to customize the generated action classes, including the Base classes.

```
php artisan vendor:publish --tag=model-actions-stubs
```

Once published, you can edit the files in `stubs/model-actions/`. The command will automatically use these custom stubs when generating new actions.

Example:

- `stubs/model-actions/Index.stub` -&gt; Customizes `UserIndexAction`
- `stubs/model-actions/base/IndexAction.stub` -&gt; Customizes `App\Actions\_Base\IndexAction`

Usage
-----

[](#usage)

### Generate Actions for a Model

[](#generate-actions-for-a-model)

```
# Generate all actions for User model
php artisan make:actions User

# Generate specific actions only
php artisan make:actions User --actions=index,store,update

# Force overwrite existing actions
php artisan make:actions User --force

# Specify custom model namespace
php artisan make:actions User --model-path=App\\Domain\\Users\\Models
```

This will create the following files in `app/Actions/User/`:

- `UserIndexAction.php`
- `UserShowAction.php`
- `UserStoreAction.php`
- `UserUpdateAction.php`
- `UserDeleteAction.php`

### Generate a Custom Action

[](#generate-a-custom-action)

Create a single custom action for a model:

```
# Basic usage - model name extracted from action name
php artisan make:action UserActivateAction

# Explicit model name
php artisan make:action ActivateAction User

# Force overwrite
php artisan make:action UserActivateAction --force
```

This creates `app/Actions/User/UserActivateAction.php` with hooks ready to use.

### Running Actions

[](#running-actions)

There are three ways to execute an action:

#### 1. Static Method (Recommended)

[](#1-static-method-recommended)

```
use App\Actions\User\UserIndexAction;
use App\Actions\User\UserShowAction;
use App\Actions\User\UserStoreAction;
use App\Actions\User\UserUpdateAction;
use App\Actions\User\UserDeleteAction;

// Index - Get paginated users
$users = UserIndexAction::run(perPage: 15);

// Index - Get all users without pagination
$allUsers = UserIndexAction::run(getAll: true);

// Index - With eager loading
$usersWithRoles = UserIndexAction::run(with: ['roles', 'permissions']);

// Show - Get single user
$user = UserShowAction::run(selectValue: '1');

// Store - Create new user
$newUser = UserStoreAction::run(data: [
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'password' => bcrypt('password'),
]);

// Update - Update existing user
$updatedUser = UserUpdateAction::run(
    data: ['name' => 'Jane Doe'],
    selectValue: '1'
);

// Delete - Delete a user
$deleted = UserDeleteAction::run(selectValue: '1');
```

#### 2. Helper Function

[](#2-helper-function)

```
use App\Actions\User\UserIndexAction;

// Using the run() helper
$users = run(new UserIndexAction(perPage: 10));

// With multiple parameters
$user = run(new UserShowAction(
    selectValue: '1',
    with: ['roles', 'posts']
));
```

#### 3. Instance Method

[](#3-instance-method)

```
use App\Actions\User\UserIndexAction;

// Create instance and execute
$action = new UserIndexAction(perPage: 10);
$users = $action->execute();

// Or using invokable
$users = $action();
```

### Customizing Actions

[](#customizing-actions)

#### Using Custom Query Builder

[](#using-custom-query-builder)

The `IndexAction` provides a `customBuilder` method for complex queries:

```
