Kritim Yantra
May 15, 2025
Laravel is more than just a framework—it's a complete developer ecosystem. One of the coolest things it offers is package development. With packages, you can create reusable, modular components that make your projects cleaner, more maintainable, and even shareable with the world.
If you’ve ever wondered how developers build Laravel packages, this blog post is for you. Let’s walk through building your first Laravel package from scratch—step by step, the beginner-friendly way.
A Laravel package is a self-contained chunk of code you can drop into any Laravel app. Think of it like a plugin. It might include routes, views, migrations, commands, or helpers. Packages help you:
Before diving in, make sure you have:
Start by creating a fresh Laravel project (if you haven’t already):
composer create-project laravel/laravel my-project
cd my-project
Now, make a directory for your package inside your Laravel project:
mkdir -p packages/YourVendor/Calculator
Replace
YourVendor
with your name or company, andCalculator
with your package name.
Inside packages/YourVendor/Calculator
, create this structure:
src/
config/
routes/
resources/views/
database/migrations/
tests/
Each folder has a purpose:
src/
– main PHP codeconfig/
– configuration filesroutes/
– web/API routesresources/views/
– Blade viewsdatabase/migrations/
– DB migrationstests/
– tests for your packageThis is the heart of your package. Create a service provider via Artisan:
php artisan make:provider CalculatorServiceProvider
Move it to your package:
packages/YourVendor/Calculator/src/CalculatorServiceProvider.php
And update it like this:
<?php
namespace YourVendor\Calculator;
use Illuminate\Support\ServiceProvider;
class CalculatorServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('calculator', function () {
return new Calculator();
});
}
public function boot()
{
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
$this->loadViewsFrom(__DIR__.'/../resources/views', 'calculator');
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
$this->publishes([
__DIR__.'/../config/calculator.php' => config_path('calculator.php'),
]);
}
}
Let’s build a simple calculator class to do basic operations.
// src/Calculator.php
namespace YourVendor\Calculator;
class Calculator
{
public function add($a, $b)
{
return $a + $b;
}
public function subtract($a, $b)
{
return $a - $b;
}
}
Facades let you use your class like Calculator::add(2, 3)
.
// src/Facades/Calculator.php
namespace YourVendor\Calculator\Facades;
use Illuminate\Support\Facades\Facade;
class Calculator extends Facade
{
protected static function getFacadeAccessor()
{
return 'calculator';
}
}
Create your config file:
// config/calculator.php
return [
'default_currency' => 'USD',
];
And don’t forget to publish it inside your service provider’s boot()
method.
Create your routes:
// routes/web.php
use Illuminate\Support\Facades\Route;
use YourVendor\Calculator\Facades\Calculator;
Route::get('calculate', function () {
$result = Calculator::add(5, 3);
return view('calculator::result', ['result' => $result]);
});
Then, make the view file:
<!-- resources/views/result.blade.php -->
<h1>Calculation Result: {{ $result }}</h1>
Update your root composer.json
:
"autoload": {
"psr-4": {
"App\\": "app/",
"YourVendor\\Calculator\\": "packages/YourVendor/Calculator/src/"
}
}
Then dump the autoload:
composer dump-autoload
Open bootstrap/providers.php
and register your service provider:
return [
// ...
YourVendor\Calculator\CalculatorServiceProvider::class,
],
Make sure everything works by writing a simple test:
// tests/Feature/CalculatorTest.php
namespace Tests\Feature;
use Tests\TestCase;
use YourVendor\Calculator\Calculator;
class CalculatorTest extends TestCase
{
public function test_addition()
{
$calc = new Calculator();
$this->assertEquals(8, $calc->add(5, 3));
}
}
Run it:
php artisan test
Also, visit /calculate
in the browser—you should see Calculation Result: 8
.
Want to publish your package? Follow these steps:
composer.json
in your package:{
"name": "your-vendor/calculator",
"autoload": {
"psr-4": {
"YourVendor\\Calculator\\": "src/"
}
}
}
composer require
it.Congratulations! You just built your first Laravel package from scratch. This is a huge milestone as a Laravel developer.
💬 Final Tip: Package development helps you think modularly. Whether you're creating internal tools for your company or open-source contributions, it’s a great way to grow your skills and stand out in the Laravel community.
Happy Coding! 🚀
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google