How to generate PHP enums from database in Laravel?

A magic number is a configuration parameter typed as a number in the middle of some code.

And that magic number is a bad thing.

Using this library, you will fix the following code:

To such:

How to use this generator?

First of…


This content originally appeared on DEV Community and was authored by Richard Dobroň

A magic number is a configuration parameter typed as a number in the middle of some code.

And that magic number is a bad thing.

Using this library, you will fix the following code:


To such:

How to use this generator?

First of all, install composer package:

composer require dobron/laravel-db-enum-generator

Let's say the user_roles database table has the following structure:

Id Slug Role
1 MANAGER Admin
2 CONTENT_CREATOR Editor
3 MODERATOR Moderator
4 ADVERTISER Advertiser
5 INSIGHTS_ANALYST Analyst

Now call artisan command make:enum.

# with laravel model
php artisan make:enum UserRoleTypes --model=UserRole --id=Id --slug=Slug --title=Role

# with laravel model that does not have slug column (will be auto-generated from --title)
php artisan make:enum UserRoleTypes --model=UserRole --id=Id --title=Role

# with database table
php artisan make:enum UserRoleTypes --table=user_roles --id=Id --slug=Slug --title=Role

# etc.

The UserRoleTypes.php file was generated in the ./app/enums/ directory.
If you update the database table, just use the same command with the --force flag to overwrite the file.

How to find magic numbers in the project?

I recommend using a library called PHPMND.

This stands for PHP Magic Number Detector.

A detailed installation guide can be found at https://github.com/povils/phpmnd

But if you're using Composer, here's how you can install it quickly:

composer require --dev povils/phpmnd

Once that's done, you run it like this:

phpmnd .


This content originally appeared on DEV Community and was authored by Richard Dobroň


Print Share Comment Cite Upload Translate Updates
APA

Richard Dobroň | Sciencx (2022-07-14T16:40:58+00:00) How to generate PHP enums from database in Laravel?. Retrieved from https://www.scien.cx/2022/07/14/how-to-generate-php-enums-from-database-in-laravel/

MLA
" » How to generate PHP enums from database in Laravel?." Richard Dobroň | Sciencx - Thursday July 14, 2022, https://www.scien.cx/2022/07/14/how-to-generate-php-enums-from-database-in-laravel/
HARVARD
Richard Dobroň | Sciencx Thursday July 14, 2022 » How to generate PHP enums from database in Laravel?., viewed ,<https://www.scien.cx/2022/07/14/how-to-generate-php-enums-from-database-in-laravel/>
VANCOUVER
Richard Dobroň | Sciencx - » How to generate PHP enums from database in Laravel?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/14/how-to-generate-php-enums-from-database-in-laravel/
CHICAGO
" » How to generate PHP enums from database in Laravel?." Richard Dobroň | Sciencx - Accessed . https://www.scien.cx/2022/07/14/how-to-generate-php-enums-from-database-in-laravel/
IEEE
" » How to generate PHP enums from database in Laravel?." Richard Dobroň | Sciencx [Online]. Available: https://www.scien.cx/2022/07/14/how-to-generate-php-enums-from-database-in-laravel/. [Accessed: ]
rf:citation
» How to generate PHP enums from database in Laravel? | Richard Dobroň | Sciencx | https://www.scien.cx/2022/07/14/how-to-generate-php-enums-from-database-in-laravel/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.