Build and publish your own Laravel package

Search for a command to run...

I could really use this package for my japanese client, it's a pain to convert the format with the kanji every time.
A few months ago, my product manager dropped something in the team channel. "We are adding an AI agent to the platform next quarter. It will handle follow-ups automatically." Just like that. One messa

f you’re diving into React, you’ve likely come across the useRef hook. But what exactly is useRef(), and how can it make your life easier? In this post we’ll cover what useRef is, walk through five

A bug that only happens for some users, that you can never reproduce, that isn't in your code. Welcome to the React + browser-translation crash — what causes it, why it's so sneaky, and the battle-tested one-file fix.

Upgrading a core production database is usually the stuff of dev nightmares. It often involves maintenance windows, scheduled downtime, and the looming fear of a botched migration. But what if you cou

We are moving into products that adapt, predict and react with real context. Designing only interfaces is not enough anymore. We are designing experiences that understand the user. Designers and digital agencies are now challenged to move beyond trad...

Engineering at JoBins
167 posts
Welcome to Engineering at JoBins — where we share the stories, insights, and lessons from building a world-class hiring platform. From system design and scalability to developer tools and team culture, our engineers write about the real challenges we solve every day. Whether you're a curious developer or a fellow builder, we hope our experiences inspire and inform your own engineering journey.
Package is a stand-alone source code which we can import into our different projects using a package management tool like a composer (in the case of PHP).
Many developers reuse the same source code by copy-pasting code in different projects. In the case of vulnerability use or bug fix, we need to fix all projects. So better build your custom package and maintain it in version control (such as git).
Let's build a simple PHP (composer) package, a Japanese date formatter.
Before starting you must have installed Composer https://getcomposer.org/doc/ and Git in your system. Scaffold a new laravel project.

Here, I create packages directory and {vendor name} directory followed by {package directory name}. The src folder will contain all of our source code and create dist for distribution files when installing your package through composer.
Navigate the src path from the command and then initialize the package folder
$ composer init
This command will ask to create a composer.json, a configuration file for the composer.

After the initialization, the system will create the composer.json file inside the package folder

To make the package installation process easy we can add service provider in our composer.json file. When someone installs our package, they typically want Laravel's service provider to be loaded automatically instead of manually adding our service provider to the config file. For this, we need to define the provider in the extra section.
"extra": {
"laravel": {
"providers": [
"Ashokgrg\\JpDateFormatter\\JpDateFormatterServiceProvider"
]
}
},
Let's create our service provider class file inside the src directory
<?php
namespace Ashokgrg\JpDateFormatter;
use Illuminate\Support\ServiceProvider;
class JpDateFormatterServiceProvider extends ServiceProvider
{
public function boot()
{
dd("Its works");
}
public function register()
{
}
}
Edit the parent composer.json file of our project define the package information
"repositories": {
"jp-date-formatter": {
"type": "path",
"url": "packages/ashokgrg/jp-date-formatter",
"options": {
"symlink": true
}
}
},
inside the require object let's include our the package
"require": {
"ashokgrg/jp-date-formatter": "@dev"
},
After that, run the composer update command in the root directory. It will create our package folder inside the root vendor directory and check the application. We can check if it works text in the browser.

JpDateFormatter.phpClassLet's create our date formatter class inside the src and define the method.
<?php
namespace Ashokgrg\JpDateFormatter;
class JpDateFormatter
{
public static function format(string $date): string
{
$year = date('Y', strtotime($date));
$month = date('m', strtotime($date));
$day = date('d', strtotime($date));
return $year."年".$month."月".$day."日";
}
}
JpDateFormatterServiceProvider.php Laravel Service Provider ClassLet assign a method from the service provider that returns singleton class
<?php
namespace Ashokgrg\JpDateFormatter;
use Illuminate\Support\ServiceProvider;
class JpDateFormatterServiceProvider extends ServiceProvider
{
public function boot()
{
}
public function register()
{
$this->app->singleton(JpDateFormatter::class, function(){
return new JpDateFormatter();
});
}
}
Let's test our package by adding a new route that returns today's date
<?php
Route::get('/test', function () {
return JpDateFormatter::format(Carbon::now());
});

Now, time to push our package in GitHub and publish our package publicly. Here I push our demo package in my GitHub account. Then log in to https://packagist.org/ (https://packagist.org/) with our account and click submit button and paste your GitHub repo and then submit.

https://laravel.com/docs/8.x/packages
https://medium.com/@andrewhanks2402/step-by-step-guide-to-laravel-package-development-82e2865fb278