PHPackages                             timwassenburg/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. [CLI &amp; Console](/categories/cli)
4. /
5. timwassenburg/laravel-service-generator

ActiveLibrary[CLI &amp; Console](/categories/cli)

timwassenburg/laravel-service-generator
=======================================

Generate Laravel services

v1.0.5(3y ago)104233.1k↓21.8%13[2 PRs](https://github.com/timwassenburg/laravel-service-generator/pulls)2MITPHP

Since Oct 24Pushed 1y ago2 watchersCompare

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

READMEChangelog (6)Dependencies (4)Versions (9)Used By (2)

[![Logo](img/banner.png)](img/banner.png)

[![Latest Version on Packagist](https://camo.githubusercontent.com/fa67f4eea601c4503d434b2e77bc3d5d14891d94e168935b1d4da305df671127/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f74696d77617373656e627572672f6c61726176656c2d736572766963652d67656e657261746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/timwassenburg/laravel-service-generator)[![GitHub Tests Action Status](https://camo.githubusercontent.com/1a5540fc354f9b0702c8fb25503ba31979d51a040b0eb0ff00cac1c1c4111f7a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f74696d77617373656e627572672f6c61726176656c2d736572766963652d67656e657261746f722f72756e2d74657374732e796d6c3f6272616e63683d6d6173746572266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/timwassenburg/laravel-service-generator/actions?query=workflow%3Arun-tests+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/26109c764b8b988fa4fdf6d2ddeb3491d9a7c20265d64dd7beedc646b192e369/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f74696d77617373656e627572672f6c61726176656c2d736572766963652d67656e657261746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/timwassenburg/laravel-service-generator)[![License](https://camo.githubusercontent.com/fe8d540171d21ac236246c16976959d876f29efee39e697e1910cd94eecfa1c9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f74696d77617373656e627572672f6c61726176656c2d736572766963652d67656e657261746f72)](https://packagist.org/packages/timwassenburg/laravel-service-generator)

---

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. [Testing](#testing)
6. [More generator packages](#more-generator-packages)
7. [Contributing](#contributing)
8. [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 quickly started.

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

[](#installation)

Install the package with composer.

```
composer require timwassenburg/laravel-service-generator --dev
```

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
```

Optionally, you can add multiple method names (seperated by comma) with the `--methods` param.

```
php artisan make:service UserService --methods=register,login,logout
```

### 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'));
    }
}
```

Testing
-------

[](#testing)

Run the tests with:

```
composer test
```

More generator packages
-----------------------

[](#more-generator-packages)

Looking for more ways to speed up your workflow? Make sure to check out these packages.

- [Laravel Action Generator](https://github.com/timwassenburg/laravel-action-generator)
- [Laravel Pivot Table Generator](https://github.com/timwassenburg/laravel-pivot-table-generator)
- [Laravel Repository Generator](https://github.com/timwassenburg/laravel-repository-generator)
- [Laravel Service Generator](https://github.com/timwassenburg/laravel-service-generator)
- [Laravel Trait Generator](https://github.com/timwassenburg/laravel-trait-generator)

The packages mentioned above are part of [Laravel Artisan Extender](https://github.com/timwassenburg/laravel-artisan-extender).

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

License
-------

[](#license)

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

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance27

Infrequent updates — may be unmaintained

Popularity50

Moderate usage in the ecosystem

Community19

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 72.2% 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.

###  Release Activity

Cadence

Every ~110 days

Recently: every ~137 days

Total

6

Last Release

1117d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/18ef476c8a9d76d44ab65724fb4be330c1c4ce90e2be793bc4628b85bd0d5ba5?d=identicon)[timwass](/maintainers/timwass)

---

Top Contributors

[![timwassenburg](https://avatars.githubusercontent.com/u/75377381?v=4)](https://github.com/timwassenburg "timwassenburg (26 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (6 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (4 commits)")

---

Tags

artisancligeneratorlaravelphpservice-patternphpclilaravelgeneratorartisanpatternservices

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

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

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

###  Alternatives

[nunomaduro/collision

Cli error handling for console/command-line PHP applications.

4.6k331.8M8.5k](/packages/nunomaduro-collision)[nunomaduro/laravel-console-menu

Laravel Console Menu is an output method for your Laravel/Laravel Zero commands.

815412.0k48](/packages/nunomaduro-laravel-console-menu)[nunomaduro/laravel-console-task

Laravel Console Task is a output method for your Laravel/Laravel Zero commands.

2582.1M11](/packages/nunomaduro-laravel-console-task)[nunomaduro/laravel-console-summary

A Beautiful Laravel Console Summary for your Laravel/Laravel Zero commands.

662.0M3](/packages/nunomaduro-laravel-console-summary)[nunomaduro/laravel-console-dusk

Laravel Console Dusk allows the usage of Laravel Dusk in Laravel/Laravel Zero artisan commands.

16255.4k7](/packages/nunomaduro-laravel-console-dusk)[rahul900day/laravel-console-spinner

Laravel Console Spinner is a spinner output for Laravel command line.

76125.4k1](/packages/rahul900day-laravel-console-spinner)

PHPackages © 2026

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