PHPackages                             raviyatechnical/laravel-service-generator - 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. raviyatechnical/laravel-service-generator

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

raviyatechnical/laravel-service-generator
=========================================

Generate services in Laravel with the artisan command

026PHP

Since Feb 11Pushed 3y ago1 watchersCompare

[ Source](https://github.com/raviyatechnical/laravel-service-generator)[ Packagist](https://packagist.org/packages/raviyatechnical/laravel-service-generator)[ RSS](/packages/raviyatechnical-laravel-service-generator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

 [![Logo](https://camo.githubusercontent.com/78b2dea84560101d2fa6fb87affe4484f0f44ee388df01d5da980cee071ee77f/68747470733a2f2f7777772e72616a746563686e6f6c6f676965732e636f6d2f75692f696d616765732f72616a2d746563686e6f6c6f676965732d6c6f676f2d746f702d70616e656c2e6a7067)](https://camo.githubusercontent.com/78b2dea84560101d2fa6fb87affe4484f0f44ee388df01d5da980cee071ee77f/68747470733a2f2f7777772e72616a746563686e6f6c6f676965732e636f6d2f75692f696d616765732f72616a2d746563686e6f6c6f676965732d6c6f676f2d746f702d70616e656c2e6a7067)Laravel **Service Generator**
=============================

[](#laravel-service-generator)

 Quickly generate services for your projects!

Table of Contents
-----------------

[](#table-of-contents)

1. [Features](#features)
2. [Installation](#installation)
3. [Usage](#usage)
    - [Generate services](#generate-services)
    - [Generate services for models](#generate-services-for-models)
    - [Generate services for controllers](#generate-services-for-controllers)
4. [The service pattern](#the-service-pattern)
    - [When to use the service pattern](#when-to-use-the-service-pattern)
    - [How to use services](#how-to-use-services)
        - [Static methods](#static-methods)
        - [Dependency Injection](#depency-injection)
5. [More generator packages](#more-generator-packages)
6. [Contributing](#contributing)
7. [License](#license)

Features
--------

[](#features)

This package adds the `php artisan make:service {name}` command. The command generates an empty service class in `app\Services` to get started. I made this mainly for own use because I like to be able to generate recurring files from the command line to keep my workflow consistent.

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

[](#installation)

Install the package with composer.

```
composer require raviyatechnical/laravel-service-generator
```

Usage
-----

[](#usage)

After installation the `php artisan make:service {name}` will be available in the list of artisan commands.

### Generate Service

[](#generate-service)

To generate a new service use the following artisan command.

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

### Generate a service for a model

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

Add a `--service` or `-S` param to generate a service for the model.

```
php artisan make:model Post --service
```

Use the `-a` or `--all` param to generate a service, migration, seeder, factory, policy, and resource controller for the model.

```
php artisan make:model Post --all
```

### Generate a service for a controller

[](#generate-a-service-for-a-controller)

Add a `--service` or `-S` param to generate a service for the controller.

```
php artisan make:controller PostController --service
```

The service pattern
-------------------

[](#the-service-pattern)

### When to use the service pattern

[](#when-to-use-the-service-pattern)

A common question is: where do I put my business logic? You want to keep your models thin and your controller functions skinny. There are multiple ways to archive this, extracting your business logic to the service layer is a common method. By encapsulating your business logic in a service class you are able to re-use the logic for example in your controllers, commands, jobs and middelware.

### How to use services

[](#how-to-use-services)

Once you have made a service it is time to add your business logic. We will discus how to use a service via static methods, dependency injection and how to use it with interfaces and repositories.

#### Static methods

[](#static-methods)

a common way to use a service is to call it's methods statically. It is similar to helper functions. Let's say we have a `PostService` with a method to get a post based on a slug.

```
namespace App\Services;

use App\Models\Post;

class PostService
{
    // Declare the function as static
    public static function getPostBySlug(string $slug): Post
    {
        return Post::with('tags')
            ->where('slug', $slug)
            ->get();
    }
}
```

Next you can include the service class for example your controller and call the `getPostBySlug` method statically.

```
namespace App\Http\Controllers;

// Include the service
use App\Services\PostService;

class PostController extends Controller
{
    public function show(string $slug)
    {
        // Call the method statically from the service class
        $post = PostService::getPostBySlug($slug);

        return view('posts.show', compact('post'));
    }
}#
```

The `getPostBySlug` method is in this example a very simple function but as you can see it keeps you controller skinny and and your business logic seperated. Keep in mind that static classes and methods are stateless. The class won't save any data in itself.

#### Dependency Injection

[](#dependency-injection)

Another popular method is to use services with dependency injection. With dependency injection you can write loosely coupled code. When done right this will improve the flexibility and maintainability of your code.

The `PostService` we used as example before will remain almost the same except we don't declare the functions inside the class as static anymore.

```
namespace App\Services;

use App\Models\Post;

class PostService
{
    public function getPostBySlug(string $slug): Post
    {
        return Post::with('tags')
            ->where('slug', $slug)
            ->get();
    }
}
```

Next we inject the service into the constructor of the class where we want to use it. Inside the constructor we assign the object to the `$postService` class property. Now the `$postService` property will be callable in all functions within the class with `$this->postService`. While typing your IDE will already typehint the functions in your PostService class, in this case only `->getPostBySlug($slug)`.

```
namespace App\Http\Controllers;

// Include the service
use App\Services\PostService;

class PostController extends Controller
{
    // Declare the property
    protected $postService;

    // Inject the service into the constructor
    public function __construct(PostService $postService)
    {
        // Assign the service instance to the class property
        $this->postService = $postService;
    }

    public function show($slug)
    {
        // Call the method you need from the service via the class property
        $post = $this->postService->getPostBySlug($slug);

        return view('posts.show', compact('post'));
    }
}
```

Contributing
------------

[](#contributing)

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

1. Fork the Project
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the Branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request

First Inspiration Package
=========================

[](#first-inspiration-package)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity23

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/484d8853ed6af9e62e0170e999ae07b1e827d31aca601953f3e0fc81286799a0?d=identicon)[raviyatechnical](/maintainers/raviyatechnical)

---

Top Contributors

[![bhargavraviya](https://avatars.githubusercontent.com/u/46953122?v=4)](https://github.com/bhargavraviya "bhargavraviya (4 commits)")

### Embed Badge

![Health badge](/badges/raviyatechnical-laravel-service-generator/health.svg)

```
[![Health](https://phpackages.com/badges/raviyatechnical-laravel-service-generator/health.svg)](https://phpackages.com/packages/raviyatechnical-laravel-service-generator)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
