personal-logo
Freelancer Web Developer in Nepal with 12+ Years of Experience

Kokil Thapa is a skilled and passionate web developer specializing in full-stack development, with a focus on creating optimized, user-friendly websites and applications for businesses and individuals.

How to Create Custom Laravel Packages

Laravel’s ecosystem thrives because of its strong package architecture. Many of the framework’s most powerful capabilities—authentication scaffolding, cashiers, permissions, multi-tenancy, debugging, API tools—are delivered through packages.
If you’re building reusable modules across multiple projects or publishing open-source tools, creating custom Laravel packages is essential.

But building a package the right way is much more than putting code inside /packages/.
You must structure the package correctly, register service providers, publish config files, create facades, manage versioning, ensure compatibility, and integrate Composer auto-loading.

This expert guide teaches you how to build robust, production-ready Laravel packages like a professional Laravel package developer.


Why Build a Custom Laravel Package?

Developers create packages for several reasons:

Reuse logic across multiple projects

Example: A multi-tenant module reused across all SaaS projects.

Open-source contribution

Boost your reputation, GitHub profile, and community trust.

Decoupling large applications

Move features into external modules for easier maintenance.

Client-specific modules

Such as integrations, billing SDKs, or company-wide core libraries.

Commercial packages

Premium components, SaaS SDKs, or add-ons for distribution.

Laravel packages increase efficiency, reduce duplication, improve maintainability, and accelerate team development.


Types of Laravel Packages

Laravel supports two primary package types:


1. Standalone Packages

These packages are framework-agnostic or usable outside Laravel, but offer Laravel integrations via service providers.

Examples:

  • league/flysystem

  • guzzlehttp/guzzle

They include optional Laravel integration.


2. Full Laravel Packages

These packages depend entirely on Laravel and extend the framework.

Examples:

  • spatie/laravel-permission

  • laravel/sanctum

  • stancl/tenancy

Most custom organizational packages fall into this category.


Step-by-Step Guide to Creating a Custom Laravel Package

We’ll build a fully structured package named:

kokil/laravel-awesome-tools

You can replace this with your own vendor and package names.


1. Create the Package Directory

Inside your Laravel project (or anywhere):

packages/ kokil/ laravel-awesome-tools/

Then create the base structure:

src/ composer.json README.md

2. Initialize Composer

Navigate to your package folder and run:

composer init

Fill in:

  • Vendor name

  • Package name

  • Description

  • Minimum stability

  • License

Your composer.json should look like:

{ "name": "kokil/laravel-awesome-tools", "description": "A custom Laravel package with powerful utilities.", "type": "library", "autoload": { "psr-4": { "Kokil\\AwesomeTools\\": "src/" } }, "require": { "php": "^8.1", "illuminate/support": "^10.0" } }

3. Register the Package in Laravel (Path Repository)

In your main Laravel project’s root composer.json:

"repositories": [ { "type": "path", "url": "packages/kokil/laravel-awesome-tools" } ]

Install it locally:

composer require kokil/laravel-awesome-tools:@dev

4. Create the Service Provider

All Laravel packages must have a service provider.

Create:

src/LaravelAwesomeToolsServiceProvider.php

Add:

namespace Kokil\AwesomeTools; use Illuminate\Support\ServiceProvider; class LaravelAwesomeToolsServiceProvider extends ServiceProvider { public function register() { // Register bindings, singletons, or configs } public function boot() { // Publish config, migrations, routes, views } }

5. Add the Provider to Composer (Automatic Discovery)

In your package’s composer.json:

"extra": { "laravel": { "providers": [ "Kokil\\AwesomeTools\\LaravelAwesomeToolsServiceProvider" ] } }

Laravel auto-discovers the provider—no need for manual registration.


6. Creating a Config File for Your Package

Inside your package:

config/awesome.php

Example:

return [ 'enabled' => true, 'api_url' => 'https://api.example.com' ];

Publish it from the service provider:

public function boot() { $this->publishes([ __DIR__.'/../config/awesome.php' => config_path('awesome.php'), ], 'awesome-config'); }

Now users can publish:

php artisan vendor:publish --tag=awesome-config

7. Adding Routes to Your Package

Create:

routes/web.php

Inside:

<?php use Illuminate\Support\Facades\Route; Route::get('awesome', function () { return 'Hello from Laravel Awesome Tools!'; });

Load it in the service provider:

$this->loadRoutesFrom(__DIR__.'/../routes/web.php');

8. Adding Views to Your Package

Create:

resources/views/index.blade.php

Load views:

$this->loadViewsFrom(__DIR__.'/../resources/views', 'awesome');

Usage:

return view('awesome::index');

9. Creating a Facade (Optional but Common)

Facades make your package nicer to use.

Create a Class:

src/AwesomeTools.php
namespace Kokil\AwesomeTools; class AwesomeTools { public function greet($name) { return "Hello, {$name}!"; } }

Bind it:

public function register() { $this->app->singleton('awesome', function(){ return new AwesomeTools(); }); }

Create Facade:

src/Facades/Awesome.php
namespace Kokil\AwesomeTools\Facades; use Illuminate\Support\Facades\Facade; class Awesome extends Facade { protected static function getFacadeAccessor() { return 'awesome'; } }

Usage:

use Kokil\AwesomeTools\Facades\Awesome; Awesome::greet('Kokil');

10. Adding Migrations to Your Package

Inside your package:

database/migrations/2025_01_01_000000_create_awesome_table.php

Load migrations:

$this->loadMigrationsFrom(__DIR__.'/../database/migrations');

11. Writing Tests for Your Package

Laravel packages can use PHPUnit:

tests/TestCase.php

Testing packages ensures version stability and compatibility.


12. Versioning and Publishing

For GitHub:

git tag v1.0.0 git push origin v1.0.0

Developers can install it with:

composer require kokil/laravel-awesome-tools

13. Bonus: Publishing Your Package on Packagist

  1. Go to https://packagist.org

  2. Submit your repo

  3. Packagist auto-updates with each tag

Your package is now publicly installable.


Expert Tips for Building Packages Like a Laravel Core Developer

Use a clean namespace structure

Organize code by domain, not by Laravel conventions.

Avoid hard dependencies

Let users customize config and behavior.

Keep your package small and modular

Single-responsibility packages get more adoption.

Use semantic versioning (SemVer)

  • Breaking changes → major version

  • New features → minor version

  • Fixes → patch

Document everything

Clear docs improve adoption.


Here are the best Laravel package development tools in 2025:


1. spatie/laravel-package-tools

Makes package creation extremely simple:

  • Automatically loads migrations

  • Automatically publishes config

  • Auto-registers commands

This package is used by Spatie for dozens of packages.


2. Orchestra Testbench

Testbench allows your package to run inside a mini Laravel environment.
Essential for writing testable packages.


3. Laravel Pint / PHPStan / Psalm

Static analysis tool for clean, maintainable package code.


4. GitHub Actions

Automate:

  • Tests

  • Code quality checks

  • Automatic tagging

  • Release pipelines


Conclusion

Creating custom Laravel packages is an essential skill for advanced developers, especially when building internal tools, scalable SaaS architectures, reusable modules, or public open-source projects.

Laravel makes package development clean, elegant, and highly extensible through:

  • Service providers

  • Automatic package discovery

  • Facades

  • Config publishing

  • Migrations

  • Routes & views loading

  • Composer autoloading

By following the steps in this guide, you can build maintainable, testable, production-ready packages that integrate seamlessly into the Laravel ecosystem.

For expert help or deep-dive package engineering, explore resources from a professional web developer in Nepal or consult a qualified multi-tenant developer experienced in building Laravel core extensions and commercial SaaS packages.

Quick Contact Options
Choose how you want to connect me: