PHP Enums in Laravel: Practical Example from Package

preview_player
Показать описание
The last video in the mini-series about Laravel Pay Pocket package, discussing how PHP Enums are used in there.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

Nice and pratically usable example, with some extra's for enums I hadn't thought about 'till now. Thanks! I think Enums are great and make the code more readable!

emmanuel-dev
Автор

I am using enum for my project. It helps if we are handling for eg. 5 status and showing as a badge with icon. It is simple to handle with enum. Just get the enum and call a class function or icon or label etc . These can be configured in the enum file.

mohamednafil
Автор

Hello, can anyone tell my why some package put the "App\Models\User" in the database?

rollandson
Автор

afaik there is no built-in method for getting all the defined values for enum cases. this is the simplest/most readable code what i can come up with

array_column(WalletEnums::cases(), 'name');

jdrab
Автор

You can turn it more complex implementing JsonSerialize when json_encoding let's say for api responses or js framework etc...

julienSibille
Автор

I've used enums in a laravel app where we had lots of statuses for a single model (eg. Book::where('status', This made the code more readable imo

MisterSammyB
Автор

Yeah actually these days enums are quite common and helpful in projects because of the ease of comparison in boolean expressions and data validation going to db.

shahsawoodshinwari
Автор

I already wrote about this topic somewhere in one of the videos. But I will repeat myself.
1. I don't understand why these are called ENUM. ENUM is a field form from DB. From the beginnings of DB, where it was important that without the "frontend option"/db management systems, more correct data would be in the DB.
2. Who thinks it is reasonable to store millions of identical text records in the database? For what? There is a "const" parameter type that can be named meaningfully. And it can be assigned an int identifier, which is then also stored in a table with much smaller resources.
const STATUS_ACTIVE = 1;
const STATUS_DISABLED = 2;
const STATUS_NOT_SET = 0;

And apply:
$x = class::STATUS_ACTIVE;

Why complicate something unnecessarily again?

Andris_Briedis
Автор

Even this might be a valid use case I still think this is so overrated. Just use lower case strings to compare what it is and has a case block to return whats the matter. No Need to create a new Class and cast it and have different methods to modify the outcome to then just further pass it as a new condition to another Model call. Wtf?

Just make a scope or $model->wallets()->business() and go ahead.

If you really want to complicate things you can always add unlimited Interfaces and Abstract Classes in front of it just to make it more complex. But tbh, this is just a string comparision method for me 10 lines, 5 lines for a conditional scope and done.

Convience me the real usage of Enums if you know how to.

gkiokan
Автор

i think its just bloating php for no reason. Doesnt really give a huge advantage.

teb