Many web applications require a similar base functionality for the backbone of the project. This can be authentication, permission handling, status and mode updates, and tracking activity. As a developer who uses a lot of Laravel in our web services, why would I spend time reinventing the wheel when I can simple
composer require it?
With that in mind, here are a few packages I instantly require on a new Laravel application.
Github user lorisleiva created a beautiful package that handles actions in Laravel in an elegant way. Laravel-Actions allows you to abstract code to an Action class, and to use that action as a controller, a job, or an action object.
When working with a new application, I try to keep my controllers as skinny as possible. This means abstracting my validation code out to FormRequest classes, extracting queries out into model scopes, and sometimes handling routing differently.
Rather than reach for my controller to edit the business logic of a process, I instead invoke a specific Laravel action, and I reach for the action to handle the business logic, along with a series of singleton classes. Laravel Actions is an elegant way to slim up your controllers if you like the fat-model, skinny-controller philosophy of development.
A Belgian-based development team, Spatie offers a truly incredible lineup of open source packages and products that work for both Laravel and generic PHP applications. They are my go-to for a lot of packages, and I use their package skeleton for my own package development. The next few packages listed will all be from Spatie. Check out their site.
Nearly all modern web applications require some level of permission handling. Spatie comes in to save the day with their Permissions package. With this package, it’s easy to assign permissions directly to users, or assign permissions to roles and then roles to users.
Spatie’s Permission works with the
web middleware in Laravel and lets you use the
@can directory in blade templates. Additionally, you can couple the permissions with the
auth middleware in your
routes/web.php file for middleware groups, setting entire endpoints to be restricted by a given permission.
The package is a lifesaver for bootstrapping permissions and roles in a new Laravel app, and it’s easy to integrate into existing projects.
Monitoring activity on applications can be a daunting task, especially as the app continues to grow. The Spatie Activity Log lets us attach a LogsActivity trait to models so that model events are logged and tracked in the database.
This package also lets us define our own custom activity logs, and set our own events and listeners for logging unique non-model-related activity in the application. Accessing a single instance of
$activity lets us access the attributes and their previous values, letting us watch changes to objects over time.
Using Model Events and Activity Log lets us access all activity related to any of our model instances across the entire application, as long as the model takes advantage of the LogsActivity trait.
Status management for an eloquent model can be straightforward. You don’t necessarily need a package to handle it. For most model states, a simple boolean for active/inactive would work just fine on the database side. Combine that with attribute casting and you’re good to go.
But if you need any kind of history of the statuses, and you want to incorporate more than just a boolean, Spatie has you covered with the Model Status package. Again, it’s a simple package that you could probably write on your own, but why spend the precious time recreating something that already exists and works out-of-the-box?
I personally like that in addition to adding statuses to models, you can also add reasoning for statuses, for instance, a model with a ‘Pending’ status can have the reason ‘needs payment’, making it easier to track steps throughout a process.
Handling Many-To-Many polymorphic relationships in Laravel can be annoying at the best of times, managing keys, relationships, and tables is often a hassle to set up. Once again, Spatie comes in to save the day. With their Laravel-Tags package, all we have to do to add taggable behavior to a model is include the appropriate trait.
Out of the box it supports translations, tag types, and tag sorting, giving us the bulk of the requirements for any tagging behavior with little to no setup on our end.
Tags can be pre-generated with database seeding, or they can be created by users and synced with model instances on post requests.
My Own Secret Ingredients
The next few packages are packages that I developed for myself, but have released for public use, as a contribution back to the open source ecosystem. I will be the first to admit that I don’t use these packages in every application, but they are always good to have on hand.
I will admit that not every application needs to have Google Analytics integration, and even so, Spatie has a great package for doing so. But cut to 2022, Google Analytics was phasing out their older versions of the API, and rolling out the GA4 protocols, which the Spatie package at the time did not support. I needed a replacement for the package, and I needed it fast, so I wrote a new one.
This GA4 package is built from the ground up to be easy as possible to generate reports from Analytics data, allowing you to access GA4 queries with easy from any point in your Laravel application.
The Period package comes shipped with the GA4 Analytics package, and is a required dependency, but you can still use it separately, and it comes in handy when using Laravel Nova, which has custom metric builders. The Period class lets you add even more flexibility to the Nova Metric builders, or any date-based querying system, by giving you the power to build complex period ranges and date ranges with simple, human-readable syntax.