Laravel Packages & Ecosystem
Laravel has a rich ecosystem of packages that extend its functionality. From debugging tools to payment integrations, the Laravel community has created thousands of packages to solve common problems. In this lesson, we'll explore popular packages and learn how to use and create them.
Understanding Packages
Packages are reusable components that add functionality to your Laravel application:
- First-party Packages: Official packages maintained by Laravel (Cashier, Scout, Horizon, etc.)
- Third-party Packages: Community-created packages available via Packagist
- Service Providers: Classes that bootstrap package functionality
- Facades: Static interfaces to classes in the service container
Note: Always check a package's documentation, GitHub stars, and last update date before using it in production. Popular, well-maintained packages are safer choices.
Installing Packages
Use Composer to install packages:
# Install a package
composer require vendor/package-name
# Install a specific version
composer require vendor/package-name:^2.0
# Install for development only
composer require --dev vendor/package-name
# Update all packages
composer update
# Update a specific package
composer update vendor/package-name
Popular Laravel Packages
1. Laravel Debugbar
Displays debug information in your browser:
# Install
composer require barryvdh/laravel-debugbar --dev
# Configuration (optional)
php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"
# Usage - automatically enabled in local environment
# Shows queries, views, routes, memory usage, etc.
2. Laravel Telescope
Powerful debugging assistant for Laravel applications:
# Install
composer require laravel/telescope
# Publish assets and migrations
php artisan telescope:install
php artisan migrate
# Access at /telescope
# Shows requests, exceptions, logs, queries, jobs, mail, etc.
# Restrict access in production
// app/Providers/TelescopeServiceProvider.php
protected function gate()
{
Gate::define('viewTelescope', function ($user) {
return in_array($user->email, [
'admin@example.com',
]);
});
}
3. Laravel Horizon
Beautiful dashboard for monitoring Redis queues:
# Install
composer require laravel/horizon
# Publish assets
php artisan horizon:install
# Start Horizon
php artisan horizon
# Access dashboard at /horizon
# Monitor queues, failed jobs, metrics, throughput, runtime, etc.
4. Laravel Socialite
OAuth authentication with Facebook, Twitter, Google, etc.:
# Install
composer require laravel/socialite
# Configure in config/services.php
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => 'http://example.com/callback',
],
# Usage
use Laravel\Socialite\Facades\Socialite;
// Redirect to provider
return Socialite::driver('github')->redirect();
// Handle callback
$user = Socialite::driver('github')->user();
$user->token;
$user->getId();
$user->getEmail();
$user->getName();
5. Laravel Scout
Full-text search for Eloquent models:
# Install
composer require laravel/scout
# Publish configuration
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
# Install search driver (Algolia, Meilisearch, etc.)
composer require algolia/algoliasearch-client-php
# Make model searchable
use Laravel\Scout\Searchable;
class Post extends Model
{
use Searchable;
public function toSearchableArray()
{
return [
'title' => $this->title,
'content' => $this->content,
];
}
}
# Search
Post::search('Laravel')->get();
6. Spatie Packages
Spatie creates many high-quality Laravel packages:
# Laravel Permission (roles & permissions)
composer require spatie/laravel-permission
# Laravel Media Library (file uploads)
composer require spatie/laravel-medialibrary
# Laravel Backup (database & file backups)
composer require spatie/laravel-backup
# Laravel Activitylog (audit trail)
composer require spatie/laravel-activitylog
# Laravel Settings (application settings)
composer require spatie/laravel-settings
7. Laravel Excel
Import and export Excel and CSV files:
# Install
composer require maatwebsite/excel
# Export
namespace App\Exports;
use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;
class UsersExport implements FromCollection
{
public function collection()
{
return User::all();
}
}
// In controller
use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;
return Excel::download(new UsersExport, 'users.xlsx');
# Import
use App\Imports\UsersImport;
Excel::import(new UsersImport, 'users.xlsx');
8. Laravel Cashier
Subscription billing with Stripe or Paddle:
# Install for Stripe
composer require laravel/cashier
# Run migrations
php artisan migrate
# Add Billable trait to User model
use Laravel\Cashier\Billable;
class User extends Authenticatable
{
use Billable;
}
# Create subscription
$user->newSubscription('default', 'price_premium')->create($paymentMethod);
# Check subscription
if ($user->subscribed('default')) {
// User is subscribed
}
Tip: Before installing any package, check its GitHub repository for stars, issues, and recent commits. Active maintenance is a good sign of a reliable package.
Creating Your Own Package
Build reusable packages for your own use or the community:
Step 1: Package Structure
vendor/package-name/
├── src/
│ ├── PackageServiceProvider.php
│ ├── Facades/
│ │ └── Package.php
│ └── Package.php
├── config/
│ └── package.php
├── resources/
│ ├── views/
│ └── lang/
├── routes/
│ └── web.php
├── tests/
├── composer.json
└── README.md
Step 2: Create composer.json
{
"name": "yourname/package-name",
"description": "Package description",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Your Name",
"email": "your.email@example.com"
}
],
"require": {
"php": "^8.1",
"illuminate/support": "^10.0"
},
"autoload": {
"psr-4": {
"YourName\\PackageName\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"YourName\\PackageName\\PackageServiceProvider"
],
"aliases": {
"Package": "YourName\\PackageName\\Facades\\Package"
}
}
}
}
Step 3: Create Service Provider
<?php
namespace YourName\PackageName;
use Illuminate\Support\ServiceProvider;
class PackageServiceProvider extends ServiceProvider
{
public function register()
{
// Merge config
$this->mergeConfigFrom(
__DIR__.'/../config/package.php', 'package'
);
// Register bindings
$this->app->singleton('package', function ($app) {
return new Package();
});
}
public function boot()
{
// Publish config
$this->publishes([
__DIR__.'/../config/package.php' => config_path('package.php'),
], 'config');
// Publish views
$this->publishes([
__DIR__.'/../resources/views' => resource_path('views/vendor/package'),
], 'views');
// Load views
$this->loadViewsFrom(__DIR__.'/../resources/views', 'package');
// Load routes
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
// Load migrations
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
// Load translations
$this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'package');
}
}
Step 4: Create Facade (Optional)
<?php
namespace YourName\PackageName\Facades;
use Illuminate\Support\Facades\Facade;
class Package extends Facade
{
protected static function getFacadeAccessor()
{
return 'package';
}
}
Step 5: Testing Your Package Locally
// In your Laravel application's composer.json
{
"repositories": [
{
"type": "path",
"url": "../path/to/package"
}
],
"require": {
"yourname/package-name": "*"
}
}
# Install the package
composer update yourname/package-name
Exercise 1: Install and Configure Popular Packages
Set up essential packages in a Laravel project:
- Install Laravel Debugbar for development
- Install Laravel Telescope and configure authentication
- Install Spatie Laravel Permission and create roles/permissions
- Install Laravel Excel and create a simple export
- Test each package to ensure proper installation
Exercise 2: Social Authentication
Implement OAuth login with Laravel Socialite:
- Install Laravel Socialite
- Register apps with GitHub and Google
- Configure services in config/services.php
- Create routes and controllers for OAuth flow
- Handle user creation/login after OAuth callback
- Test login with both providers
Exercise 3: Create a Simple Package
Build your own Laravel package:
- Create a package structure with composer.json
- Create a service provider that registers a helper function
- Add a config file that can be published
- Create a Blade component that can be used in views
- Test the package locally in a Laravel application
- Write documentation in README.md
Package Discovery Resources
- Packagist:
packagist.org - Official Composer package repository
- Laravel News:
laravel-news.com - Latest packages and tutorials
- Packalyst:
packalyst.com - Laravel package directory
- Laravel.io:
laravel.io - Community forum and resources
- Awesome Laravel: GitHub list of curated Laravel resources
Best Practices
- Evaluate Before Installing: Check stars, issues, and maintenance status
- Read Documentation: Always read package docs before using
- Keep Updated: Regularly update packages for security and features
- Semantic Versioning: Specify version constraints in composer.json
- Development vs Production: Use --dev flag for development-only packages
- Test After Updates: Run tests after updating packages
- Minimize Dependencies: Don't install packages for simple functionality you can write yourself
- Security: Regularly check for security advisories on installed packages
Summary
In this lesson, you learned:
- How to install and manage packages with Composer
- Popular Laravel packages and their use cases
- Laravel Debugbar and Telescope for debugging
- Laravel Horizon for queue monitoring
- Laravel Socialite for OAuth authentication
- Laravel Scout for full-text search
- Spatie packages for common functionality
- How to create your own Laravel packages
- Best practices for package management
The Laravel ecosystem is one of its greatest strengths. Learning to leverage existing packages saves time and ensures you're using battle-tested solutions. When you need custom functionality, creating your own package allows for reusability across projects.