Skip to content

A simple way to collect details related to a generic Model

License

Notifications You must be signed in to change notification settings

massimomarazzi/laravel-entity-details

 
 

Repository files navigation

Entity detail helper

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

laravel-entity-details is a package to create and attach some details to a generic Model in a blink of an eye 😎

Installation

You can install the package via composer:

composer require danielebarbaro/laravel-entity-details

You can publish and run the migrations with:

php artisan vendor:publish --tag="entity-details-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="entity-details-config"

This is the contents of the published config file:

return [
    'table_name' => env('ENTITYDETAIL_TABLE_NAME', 'entity_details'),
    'returns_soft_deleted_models' => env('ENTITYDETAIL_SOFTDETED_ENABLE', false),
];

Usage

Add the traits EntityDetail and EntityDetailHydrate in your related Model:

class User extends Authenticatable
{
    [...]
    use EntityDetail, EntityDetailHydrate;
    [...]
}

[...]

class Biker extends Model
{
    [...]
    use EntityDetail, EntityDetailHydrate;
    [...]
}

[...]

class Company extends Model
{
    [...]
    use EntityDetail, EntityDetailHydrate;
    [...]
}

In your Controller you can simply save or update detail with the method syncDetail($detail):

    [...]
    $user = User::create([
        'name' => 'John',
        'email' => 'Doe',
        'password' => Hash::make('password')
    ]);
    
    $user->syncDetail([
        'is_company' => true,
        'status' => 1,
        'code' => 'CODE',
        'name' => 'DUMMY COMPANY',
    ]);
    
    $user->fresh('detail');

    $detail = $user->detail;
    [...]

or

    [...]
    $company = Company::with('detail')->first();
    
    $company->syncDetail([
        'is_company' => true,
        'status' => 1,
        'code' => 'CODE',
        'name' => 'DUMMY COMPANY',
    ]);

    $detail = $company->detail;
    [...]

Relations

Traits help you to walk into relations:

    $detail = Detail::with('owner')->get();
    $user = $detail->owner;

    [...]

    $biker = Biker::with('detail')->first();
    $detail = $biker->detail;
    [...]

Scopes

Traits help you to use company and owner scope:

    $details = Detail::where('status', 1)->isCompany()->get();
    [...]
    
    $user = User::where('email', '[email protected]')->first();
    $detail = Detail::forOwner($user)->first();
    [...]

Rules

Basic rules are:

$rules = [
    'is_company' => 'required|boolean',
    'status' => 'required|max:20',
    'code' => 'required|max:10',
    'name' => 'string|max:100',
    'secondary_email' => 'email',
    'sdi' => 'max:7',
    'pec' => 'email',
    'first_name' => 'string|max:60',
    'last_name' => 'string|max:60',
    'phone' => 'string|max:60',
    'mobile' => 'string|max:60',
    'fiscal_code' => 'string|max:16',
    'vat' => 'string|max:13',
    'postal_code' => 'string|max:6',
    'city' => 'string|max:30',
    'country' => 'string|max:2',
    'address' => 'string|max:100',
    'notes' => 'string',
];

You can overwrite the $rules as you wish in your model, the validator will do the rest

Migration

Feel free to add or delete fields for Detail entity but remember to edit also the $rules.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

Thanks a lot to Spatie ❤️ for making my life easier

License

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

About

A simple way to collect details related to a generic Model

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 94.4%
  • Shell 5.6%