We, at CodeHunger (a web solutions and mobile app development company), understand the importance and
results of having aptly developed websites with great web design and user experience. Our web
development is optimized for search engines to get more website traffic or you can say SEO friendly,
for social media and digital marketing.
Contact Us
Our Company is located at the heart of bihar patna city, Our Company Address is Rai Jai Krishna Road,
Gurhatta Patna City, Near Shiv Mandir - 800008
How to Create Custom Packages in Laravel 12: A Complete Step-by-Step Guide
Learn how to build, test and publish your own custom Laravel 12 packages with this developer-friendly tutorial. Master package development to enhance code reusability and contribute to the Laravel ecosystem. Follow our expert step-by-step process from initial setup to Packagist submission.
What You'll Learn
Setting up a package development environment
Creating the package structure
Implementing service providers
Adding controllers, models, and migrations
Publishing assets and configurations
Testing your package
Submitting to Packagist'
Prerequisites
PHP 8.2 or higher
Composer installed
Basic knowledge of Laravel framework
Laravel 12 project for testing
Step 1: Set Up Your Development Environment
First, create a new Laravel 12 project where you'll develop and test your package:
Note: Ok, Now In the below part I am using CodeHunger as the packages list and Learning as the Package name, for example, the directory structure will be like this packages/YourName/yourpackage/src , then in below example it will be packages/CodeHunger/Learning/src
Step 2: Create Package Structure
Laravel packages follow a specific directory structure. Create this structure in your project's root directory:
mkdir -p packages/CodeHunger/Learning/src
Now, Create the below folder under your src Folder:
packages/
└── CodeHunger/
└── Learning/
└── src/
├── Console/
│ └── Commands/
├── Config/
├── Controllers/
├── Database/
│ ├── migrations/
│ └── seeders/
│ └── LearningSeeder.php
├── Http/
│ ├── Controllers/
│ └── Middleware/
├── Models/
│ └── Learning.php
├── Provider/
├── Resources/
│ ├── views/
│ └── lang/
├── Routes/
└── Support/
Step 3: Create Service Provider
The service provider is the entry point of your package. Create a file named LearningServiceProvider.php in the src directory:
<?php
namespace CodeHunger\Learning\Provider;
use Illuminate\Support\ServiceProvider;
class LearningServiceProvider extends ServiceProvider
This repository includes all the necessary structure and files required to build and register a Laravel 12 custom package, as detailed in the blog. Using this codebase ensures consistency with the tutorial and allows for quicker integration into your Laravel application.
Conclusion
Creating custom packages in Laravel 12 is a powerful way to modularize your code and share functionality across projects. By following this guide, you can build well-structured, maintainable packages that integrate seamlessly with Laravel's ecosystem. As you become more experienced with package development, you'll discover more advanced techniques to enhance your packages and contribute to the Laravel community. Remember that the best packages solve specific problems elegantly while maintaining compatibility with Laravel's design principles and philosophy.