Laravel 10 package development from scratch

Ivan Kolodii
4 min readApr 9

This article is an updated version of Laravel 7 package development article

Step 1. Create a new project

I prefer using the Laravel installer. You can read more about the installation here: https://laravel.com/docs/10.x/installation

laravel new lara-dg

I prefer using SQLite for testing and package development. Adjust your.env file

DB_CONNECTION=sqlite
DB_DATABASE=/Users/ivan/code/packages/test-project/database/database.sqlite

Then let’s create our package skeleton. We will be using a CLI tool. It will generate all necessary files

composer require jeroen-g/laravel-packager

Then you can run generation command. Webkid stands for vendor name(your namespace) and LaravelDiagnostic as the name of your project

php artisan packager:new Webkid LaravelDiagnostic --i

Then type information about your package and you. It should look like that

Console output

Now you have packages folder inside of your project with Webkid directory inside and all necessary files inside.

This package is automatically loaded via composer. I prefer using a relative path:

"repositories": {
"webkid/laraveldiagnostic": {
"type": "path",
"url": "../../packages/lara-dg/packages/Webkid/LaravelDiagnostic"
}
}

Also, update your require section if it wasn’t updated automatically. It will create a symlink in your vendor directory to your package directory

"require": {
"webkid/laraveldiagnostic": "*"
},

Now you can create classes inside the src folder, but they should have appropriate namespace Webkid\LaravelDiagnostic.

For example, I have Commands folder and RunDiagnostic.php class inside. So it should have such namespace:

namespace Webkid\LaravelDiagnostic\Commands;

If you want to use your migrations, routes, config files, views, or even assets(js, CSS, images), you have to load them in your service provider. You can read about that in the official documentation. I was using only commands in my example. Take…

Ivan Kolodii

Recommended from Medium

Lists

See more recommendations