How to handle enums in Laravel database

Panjeh
2 min readMar 13, 2021

--

Before you start this tutorial, I would like to introduce two packages for Laravel that I have recently developed: Laravel Pay Pocket, a modern multi-wallet package, and Laravel Failed Jobs, a UI for the Laravel Failed Jobs Table. I hope they may be of help to you.

https://github.com/HPWebdeveloper/laravel-pay-pocket
https://github.com/HPWebdeveloper/laravel-failed-jobs

Suppose there are two models Patient and also Exam and the relationship between them is One To Many, I mean each patient can have many exams.

Patient Model
Exam Model

Each exam individually has some features, For example radiologists want to add the following information for each exam: Diagnosis (Pnemonia, Tuberculosis, Normal), Degree of severity (Severe, Moderate, Mild, Absent) and Level of normality of the exam (Typical or Atypical). The question is that what would the final structure of exam table in the database look like?

In the directory config I make a exams.php file which includes the following content that finally returns an array like:

Then in the migration file of table exams we can use enum type and refer to the config file exams.php and the related keys by dot notation like :

config(exams.degree_of_severity)

and the patients table looks like this:

After doing

php artisan migrate:fresh

The table exams in the database will have such structure like:

But before using enum I recommend you read this article.

--

--

Panjeh
Panjeh

Written by Panjeh

Posting about Python and Laravel

No responses yet