PHPackages                             lambertns/laravel-make-service - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. lambertns/laravel-make-service

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

lambertns/laravel-make-service
==============================

Adds `php artisan make:service` command to generate service classes with optional automatic controller injection using PHP 8+ property promotion.

v1.2.0(6mo ago)09MITPHPPHP &gt;=8.1

Since Oct 26Pushed 6mo agoCompare

[ Source](https://github.com/Lambertn33/laravel-make-service)[ Packagist](https://packagist.org/packages/lambertns/laravel-make-service)[ RSS](/packages/lambertns-laravel-make-service/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (3)Used By (0)

Laravel Make Service
====================

[](#laravel-make-service)

A Laravel package that adds a `make:service` command to generate service classes with optional dependency injection into controllers.

Why Use Services?
-----------------

[](#why-use-services)

Controllers can quickly become bloated with business logic, making them hard to maintain and test. Services help by:

- 🔧 **Separate Concerns**: Move business logic out of controllers into dedicated service classes
- 📉 **Reduce Controller Complexity**: Keep controllers thin and focused on HTTP handling
- ♻️ **Reusability**: Share business logic across multiple controllers or queue jobs
- 🧪 **Testability**: Easier to unit test business logic in isolation
- 📚 **Maintainability**: Organized code that's easier to read and maintain

### Example Problem

[](#example-problem)

**Without Services (Fat Controller):**

```
class OrderController extends Controller
{
    public function store(Request $request)
    {
        // Validation
        $validated = $request->validate([...]);

        // Business logic
        $order = Order::create($validated);

        // Payment processing
        $payment = PaymentGateway::charge(...);
        $order->update(['payment_id' => $payment->id]);

        // Notification logic
        Mail::to($order->user)->send(new OrderConfirmation($order));

        // Inventory management
        foreach ($order->items as $item) {
            $item->product->decrement('stock', $item->quantity);
        }

        // More logic...

        return response()->json($order, 201);
    }
}
```

**With Services (Clean Controller):**

```
class OrderController extends Controller
{
    public function __construct(
        private OrderService $orderService
    ) {}

    public function store(Request $request)
    {
        $order = $this->orderService->createOrder($request->validated());

        return response()->json($order, 201);
    }
}
```

Features
--------

[](#features)

- 🚀 Generate service classes with the `php artisan make:service` command
- 💉 Automatically inject services into controllers using the `--controller` option
- 📝 Use PHP 8+ constructor property promotion for clean dependency injection
- 🎯 Add multiple methods to services using the `--methods` option
- ✅ Follows Laravel conventions and best practices

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

[](#installation)

You can install the package via Composer:

```
composer require lambertns/laravel-make-service
```

That's it! The package will automatically register the service provider.

Quick Start
-----------

[](#quick-start)

Let's say you have a controller with lots of business logic and want to refactor it:

1. **Create a service to extract the logic:**

```
php artisan make:service PaymentService --controller=OrderController --methods="process,refund,cancel"
```

2. **Your service is created and automatically injected:**

```
// app/Http/Services/PaymentService.php
class PaymentService
{
    public function process($order) { /* ... */ }
    public function refund($order) { /* ... */ }
    public function cancel($order) { /* ... */ }
}
```

3. **Use it in your controller:**

```
// app/Http/Controllers/OrderController.php
class OrderController extends Controller
{
    public function __construct(
        private PaymentService $paymentService
    ) {}

    public function store(Request $request)
    {
        // Now your business logic is in the service
        $order = $this->paymentService->process($request->validated());

        return response()->json($order, 201);
    }
}
```

Usage
-----

[](#usage)

### Basic Service Creation

[](#basic-service-creation)

Create a simple service:

```
php artisan make:service UserService
```

This will create `app/Http/Services/UserService.php`:

```
