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"
This section is for when you want to set all, or some of your attributes to uppercase.
// ...
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.
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.
- 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. - 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. - In addition, a custom rule is available for the
exists
andunique
rules that add thewhere
condition for the authenticated user, for you. It can also concatenate more conditions and other functionalities of the base rulesexists
andunique
.
// ...
use Luilliarcec\Utilities\Concerns\BelongsToAuthenticated;
class Invoice extends Model
{
use BelongsToAuthenticated;
}
Invoice::withoutAuthenticated()
// ->where(...)
->get();
use Luilliarcec\Utilities\Rules\Authenticated;
Request::validate([
'invoice_id' => Authenticated::make('invoices', 'id')->exists() // ->where(...)
]);
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
]);
composer test
Please see Releases for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email luilliarcec@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.