luilliarcec/laravel-utilities

Laravel Utilities it's a package that includes common utilities for projects.

3.1.0 2023-09-24 01:31 UTC

This package is auto-updated.

Last update: 2024-11-24 04:11:28 UTC


README

run-tests Latest Version on Packagist Total Downloads GitHub license

Installation

You can install the package via composer:

composer require luilliarcec/laravel-utilities

Now publish the configuration file into your app's config directory, by running the following command:

php artisan vendor:publish --provider="Luilliarcec\Utilities\UtilitiesServiceProvider"

Table of Contents

Set Attributes Uppercase

This section is for when you want to set all, or some of your attributes to uppercase.

Usage

// ...
use Luilliarcec\Utilities\Concerns\SetAttributesUppercase;

class User extends Authenticable
{
    use SetAttributesUppercase;
}

If you want to ignore some attributes of your model, it can be set in the dontApplyCase property as follows.

// ...
use Luilliarcec\Utilities\Concerns\SetAttributesUppercase;

class User extends Authenticable
{
    use SetAttributesUppercase;
    
    protected $dontApplyCase = [
        'username'
    ];
}    

If you want to ignore attributes globally, add them in the utilities config file under the attributes_ignored_globally key.

Belongs To Auth

This section is useful when you have tables in your DB model that are related to the authenticated user. By default, the name 'user_id' is used as the foreign key for the relationship, but you can change it from the utilities configuration file in the auth_foreign_id_column key.

You can use the BelongsTo Auth Trait.

  1. This Trait will add a listener for the creating event to associate the authenticated user with the model in question when it is being created.
  2. Also add a global scope to retrieve all the records associated with the authenticated user, you can disable this scope by calling the withoutAuth function when building your query.
  3. In addition, a custom rule is available for the exists and unique rules that add the where condition for the authenticated user, for you. It can also concatenate more conditions and other functionalities of the base rules exists and unique.

Using Trait

// ...
use Luilliarcec\Utilities\Concerns\BelongsToAuthenticated;

class Invoice extends Model
{
    use BelongsToAuthenticated;
}

Using withoutAuth function

Invoice::withoutAuthenticated()
    // ->where(...)
    ->get();

Using Rules

use Luilliarcec\Utilities\Rules\Authenticated;

Request::validate([
    'invoice_id' => Authenticated::make('invoices', 'id')->exists() // ->where(...)
]);

Decimals Rule

If you want to check decimal numbers, and the number of decimal places, you can use this rule as follows.

use Luilliarcec\Utilities\Rules\Decimals;

Request::validate([
    'amount' => new Decimals // by default they are 8 integers and 2 decimals
]);

Request::validate([
    'amount' => new Decimals(20, 4) // 20 integers and 4 decimals
]);

Testing

composer test

Releases

Please see Releases for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email luilliarcec@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.