PHP code example of lambertns / laravel-make-service

1. Go to this page and download the library: Download lambertns/laravel-make-service library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lambertns / laravel-make-service example snippets


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);
    }
}

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);
    }
}

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

// 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);
    }
}



namespace App\Http\Services;

class UserService
{
    // Add your methods here
}



namespace App\Http\Services;

class PaymentService
{
    /**
     *pay()
     */
    public function pay()
    {
        // TODO: implement pay()
    }

    /**
     *refund()
     */
    public function refund()
    {
        // TODO: implement refund()
    }

    /**
     *cancel()
     */
    public function cancel()
    {
        // TODO: implement cancel()
    }
}



namespace App\Http\Controllers;

use App\Http\Services\PaymentService;

class AuthController
{
    public function __construct(
        private PaymentService $paymentService
    ) {
    }
}

public function __construct(
    private UserService $userService,
    private NotificationService $notificationService  // ← Added automatically
) {
}

public function __construct(
    private PaymentService $paymentService
) {
}



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Services\PaymentService;  // ← Added automatically

class AuthController
{
    // ...
}
bash
php artisan make:service PaymentService --controller=OrderController --methods="process,refund,cancel"
bash
php artisan make:service UserService
bash
php artisan make:service PaymentService --methods="pay,refund,cancel"
bash
php artisan make:service PaymentService --controller=AuthController
bash
php artisan make:service PaymentService --controller=AuthController,UserController,OrderController
bash
php artisan make:service NotificationService --controller=UserController
bash
php artisan make:service OrderService --methods="create,update,cancel" --controller=OrderController,CheckoutController
bash
php artisan make:service Admin\UserService --controller=Admin\UserController