Skip to content

mennovanhout/laravel-model-constants

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Model Constants

This package will generate constant files for your models to reduce typo's and the need of debugging to find out what attributes are available.

Benefits

  1. This package is ready to be installed and to forget about, no maintenance or commands you have to learn.
  2. This packages listens to the Laravel migrations ended event (Files automatically get updated/generated).
  3. When a column is removed you will get IDE errors.
  4. Fewer typo's.
  5. These constants are widely available throughout your whole codebase instead of having to type string.
  6. This package is created with all design patterns in mind. Whether you use domain driven design or Laravels default approach.

How to install it

composer require mennovanhout/laravel-model-constants

How to use it

This packages hooks into Laravels migration system and will generate the files after each migration or batch of migrations.

You can run it manually with: artisan model:constants.

If you want to remove files generated by this packages you can do that with: artisan model:constants-clean.

Example: Generated constant file

<?php

namespace Domain\Authentication\Models;

use MennoVanHout\LaravelModelConstants\Types\ModelAttributes;

class UserAttributes extends ModelAttributes
{
	 const ID = 'id';
	 const NAME = 'name';
	 const EMAIL = 'email';
	 const EMAIL_VERIFIED_AT = 'email_verified_at';
	 const PASSWORD = 'password';
	 const REMEMBER_TOKEN = 'remember_token';
	 const CREATED_AT = 'created_at';
	 const UPDATED_AT = 'updated_at';
}

Example Model

<?php

namespace Domain\Authentication\Models;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $fillable = [
        UserAttributes::NAME,
        UserAttributes::EMAIL,
        UserAttributes::PASSWORD,
    ];

    protected $hidden = [
        UserAttributes::PASSWORD,
        UserAttributes::REMEMBER_TOKEN,
    ];

    protected $casts = [
        UserAttributes::EMAIL_VERIFIED_AT => 'datetime',
    ];
}

Todo:

  • Add config file.
  • Be able to put the generated constant files in another directory.
  • Add an option to also generated relations in the constants.
  • Add an option to also generated custom attributes in the constants.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages