khant-nyar/service-extender

Creating a service base crud quickly

v1.0.0 2025-02-22 11:03 UTC

This package is auto-updated.

Last update: 2025-02-22 11:08:08 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

  • ✅ Improved Type Safety – Helps with debugging and prevents invalid data types.
  • ✅ IDE-Friendly Navigation – Ctrl+Click methods to jump to service class.
  • ✅ Automatic Transactions – Prevents database inconsistencies.
  • ✅ Logging Enabled – Tracks changes for debugging.
  • ✅ Easily Extendable – New services can override methods as needed.

Installation

You can install the package via composer:

composer require khant-nyar/service-extender

Usage

/** 
 * Example uses with UserService 
 */

namespace App\Services;

use App\Models\User;
use KhantNyar\ServiceExtender\Services\EloquenceService;

class UserService extends EloquenceService
{
    protected static string $model = User::class;
}

it will generate automacally for UserService::all(),find($id),create($data)

/**
 * Example uses in controller
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\UserService;

class UserController extends Controller
{
    public function index()
    {
        return response()->json(UserService::all());
    }

    public function show($id)
    {
        return response()->json(UserService::find((int) $id));
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:6'
        ]);

        return response()->json(UserService::create($data), 201);
    }

    public function update(Request $request, $id)
    {
        $data = $request->validate([
            'name' => 'sometimes|string|max:255',
            'email' => 'sometimes|email|unique:users,email,' . $id,
            'password' => 'sometimes|min:6'
        ]);

        return response()->json(UserService::update((int) $id, $data));
    }

    public function destroy($id)
    {
        return response()->json(['success' => UserService::delete((int) $id)]);
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email khantnyar.dev@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.