Skip to content

Pijler/laravel-common

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

25 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“Œ Laravel Common

A Laravel package that contains common functionalities I use in almost all projects I develop. This package includes traits, helpers, macros, commands, and other utilities that speed up development.

πŸ“¦ Installation

You can install the package via Composer:

composer require pijler/laravel-common

The package will be automatically discovered by Laravel.

🧩 Features

🎯 Actions

Abstract base class for executing actions in a clean and organized way:

use Common\Support\Action;

class CreateUserAction extends Action
{
    public function __construct(
        private string $name,
        private string $email
    ) {}

    protected function handle()
    {
        return User::create([
            'name' => $this->name,
            'email' => $this->email,
        ]);
    }
}

// Usage
$user = CreateUserAction::execute(
    name: 'JoΓ£o Pedro',
    email: 'joao@example.com',
);

// With conditions
CreateUserAction::executeIf($shouldCreate, 'JoΓ£o Pedro', 'joao@example.com');
CreateUserAction::executeUnless($shouldNotCreate, 'JoΓ£o Pedro', 'joao@example.com');

πŸ” Two-Factor Authentication

Trait for implementing two-factor authentication:

use Common\Traits\HasTwoFactor;

class User extends Model
{
    use HasTwoFactor;
}

// Check if user has 2FA enabled
$user->hasTwoFactor();

// Get recovery codes
$codes = $user->recoveryCodes();

// Replace recovery code
$user->replaceRecoveryCode($oldCode);

// Get QR Code SVG
$qrCode = $user->twoFactorQrCodeSvg();

// Get QR Code URL
$url = $user->twoFactorQrCodeUrl();

πŸ“± User Agent Detection

Class for detecting browser and device information:

use Common\Support\Agent;

$agent = new Agent();

// Device information
$agent->isMobile();
$agent->isTablet();
$agent->isDesktop();

// Browser information
$agent->browser(); // Chrome, Firefox, Safari, etc.

// Operating system information
$agent->platform(); // Windows, macOS, Linux, etc.

🚨 Alert System

Alert system with typed exceptions:

use Common\Enum\Alert;
use Common\Exceptions\Alert\InfoException;
use Common\Exceptions\Alert\ErrorException;
use Common\Exceptions\Alert\WarningException;

// Throw alert exceptions
InfoException::make('Info Message!');
ErrorException::make('Error Message!');
WarningException::make('Warning Message!');

// Helpers to check exceptions
alert_check_exception($exception); // bool
alert_throw_exception($exception); // void

πŸ“¨ Storage Channel

Notification channel that saves emails to files and database:

use Common\Channel\StorageChannel;

// Configure callback for custom path
StorageChannel::storagePathUsing(function ($notification) {
    return "/custom/path/{$notification->id}.html";
});

// Use in notifications
class WelcomeNotification extends Notification
{
    public function via($notifiable)
    {
        return ['storage'];
    }
}

πŸ› οΈ Macros

Useful macros for Eloquent, RedirectResponse and TestResponse:

Eloquent Builder
// Get first random record
User::firstRandom();
RedirectResponse
// Alert messages
return redirect()->info('Info Message!');
return redirect()->error('Error Message!');
return redirect()->success('Success Message!');
return redirect()->warning('Warning Message!');

// Custom message
return redirect()->message('Message text', Alert::INFO);

// Custom action
return redirect()->action(ActionData::from([
    'text' => 'Undo',
    'method' => 'patch',
    'url' => "/users/{$user->id}/restore",
]));
TestResponse
// Message assertions
$response->assertInfoMessage('Info Message!');
$response->assertErrorMessage('Error Message!');
$response->assertSuccessMessage('Success Message!');
$response->assertWarningMessage('Warning Message!');

// Action assertion
$response->assertAction(ActionData::from([
    'text' => 'Undo',
    'method' => 'patch',
    'url' => "/users/{$user->id}/restore",
]));
Inertia.js (if available)
// Automatic filters
return Inertia::render('Users/Index')->filters([
    'role' => 'admin',
    'status' => 'active',
]);

// Pagination parameters
return Inertia::render('Users/Index')->params([
    'page' => 1,
    'limit' => 10,
    'sort' => 'name',
]);

πŸ—„οΈ Database Utilities

Rename Migrations Command
php artisan common:rename-migrations

This command renames migration files to follow a consistent pattern.

🎨 Enum Helpers

Trait for enums with useful methods:

use Common\Traits\EnumMethods;

enum Status: string
{
    use EnumMethods;

    case ACTIVE = 'active';
    case INACTIVE = 'inactive';
}

// Available methods
Status::keys(); // ['ACTIVE', 'INACTIVE']
Status::values(); // ['active', 'inactive']

πŸ“ Media Library Extensions

Extensions for Spatie Media Library:

  • CustomFileNamer: Custom file naming
  • CustomPathGenerator: Custom path generation

πŸ”— Notification URL

Trait for generating notification URLs:

use Common\Traits\NotificationUrl;

class User extends Model
{
    use NotificationUrl;
}

// Generate URL for notification
$url = $user->notificationUrl($notification);

πŸ—οΈ Builder Helpers

Trait for adding useful methods to Eloquent Builders:

use Common\Traits\HasBuilder;

class User extends Model
{
    use HasBuilder;
}

// Methods available automatically on builders
User::query()->whereActive();
User::query()->whereInactive();

⚑ Horizon Queue

Trait for working with Laravel Horizon:

use Common\Traits\HorizonQueue;

class ProcessDataJob implements ShouldQueue
{
    use HorizonQueue;
}

πŸ“ License

Open-source under the MIT license.

πŸš€ Thanks!

This package contains common functionalities I use in my Laravel projects. Feel free to use and contribute!

About

Simple package with several common features in Laravel applications.

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages