How to Create a Laravel Package and How to Publish Laravel Package
Laravel is a popular PHP framework known for its neat syntax and robust features. Laravel support for package development, which allows developers to create reusable components. In this post, we will guide you through the process of creating a Laravel package and publishing it on Packagist, the main repository for PHP packages. So, let’s get started!
Step 1: Setting up the Package
First, we need to install Laravel by using Composer by running the following command:
composer global require laravel/installer
Step 2: Create the Package Structure
- Inside your Laravel project’s directory, create folder “Packages” you can use any name which you want.
cd packages
Step 3: Initialize a Composer Package
Initialize a new Composer package within your package directory. by running bellow command.
composer init
Follow the prompts to provide details such as the package name, version, description, author, etc.
Open the package’s composer.json file. Add the following lines to the “autoload” section, replacing “{namespace}\{
PackageName }” with your desired namespace:
{
"name": "your-vendor/my-package",
"description": "Your package description",
"type": "library",
"authors": [
{
"name": "Your Name",
"email": "your-email@example.com"
}
],
"require": {
"php": "^8.0",
},
"autoload": {
"psr-4": {
"YourVendor\MyPackage\": "src/"
}
}
}
Add your package’s files to the “src” directory. You can create necessary files here.
Step 4: Update the Composer Autoloader
Go back to the root directory of your Laravel project. and
Update the Composer autoloader to identify your package.
composer dump-autoload
Step 5: Test and Use Your Package
Add your package as a dependency to the Laravel project’s composer.json file.
"require": {
"vendor-name/package-name": "version"
}
Replace “vendor-name/package-name” with your package’s name
Run composer update
in the Laravel project’s root directory to install your package.
How To Publish on Laravel Package On Packagist ?
Packagist is the package repository for PHP, and publishing your Laravel package there will make it easily accessible to the Laravel community. Follow these steps to publish your package on Packagist:
Before Publishing You need to update your package repository on github.com

- Create an account on Packagist (if you don’t have one already) by visiting https://packagist.org.
- Once logged in, click on the “Submit” link at the top of the page.
- Provide the GitHub repository URL of your package and click “Check”.
- If the repository is valid, provide the necessary information and click “Submit Package”.
Once all steps done successfully. then your package will be available to use for all Laravel community.