Laravel and PHP Refactor: If-Else to Switch or Match

preview_player
Показать описание
I've received an email asking how to shorten the code with if-else statements. Let's try to optimize.

- - - - -
Support the channel by checking out our products:
Рекомендации по теме
Комментарии
Автор

Never use $model = 'App\\Models\\Users', because of the high chance of typo, explained in the video.
Instead use User::class (the same way you use in route files).
In this case User::class returns exactly fully classified class name (with namespace).

alexaverkiyev
Автор

Maybe not relevant, but I don't recommend accessing request inputs like $request->name because there are some public properties inside (which Laravel's Request depends on) like query, server, request, attributes, files, etc. They have higher priority than your inputs, and if your entry has the same name as one of them, you will have a name conflict. So always use $request->input('name') to avoid name conflicts. It also has extra features such as default value, access to arrays with dot notation.

mgsmus
Автор

Looks good ..another suggestion would be to use factory pattern where the logic resides in seperate classes like userclass and projectclass and a factory manager will have the if statements given instances of these classes .it may be over engineering for small usecases but as project grows it will be better to just implement more classes for more future types what are your thoughts ..

ashay
Автор

Once I looked at your larastarter kit and found few lines of code that may look clean if using match :)

raziuldev
Автор

Your solution is good-- But I prefer to use this -- Is this the correct way?
No need if switch or match

$modes = [
'users' => User::class,
'projects' => Project::class,
];

echo
exit;

SuperJawad
Автор

I do not agree with hard-coding the class names. You used an example where you talked about an exception or error that was thrown because someone made a typo in the Users model class name. If you use User::class, you know it cannot be a typo because you must've imported that class and there is no need for a try/catch. It is also much cleaner and readable.

stianscholtz
Автор

Thanks for great videos
i have question can you explain php opcache in laravel to improve speed of code

fadllabanie
Автор

You can use User::class and Project::class instead of string FQCN. After that you can remove unnecessary try ... catch

TsAex
Автор

I prefer like this:
return match($request->type) {
'users' => User::all(),
'projects' => Project::all(),
default => dd('Type not found')
};

wahyu_pratamabwi
Автор

To avoid model error we can check it in request class, for the possible values

andreymikhalev
Автор

Sir i am big fan your channel used every lesson you taught in production. Can you make a video on websockets in laravel that library works in localhost but sucks in production and is it only way of Asynchronous programming in php ?

nisargsachinsaple
Автор

Just curious, why not User::class instead of "App\\Models\\User"? It's much more cleaner and less error prone.

SowrenSen
Автор

What method we can use instead of all()? all is giving all the colums

krishnanarasimha
Автор

Hi Povilas, do you have any videos on advanced seeding? I haven’t seen any real tutorials on how to handle seeding when models are deeply dependent on another, for example I have users, with a company, this company has offers, and the offers have subscriptions.

lassestube
Автор

Hello, thanks for your videos, very helpful. I want to ask about this "Class not found" error. Is there any way to check if class exists? For example if I for some reason build class name dynamically and if there is no such class I return some default class.
I use this:
try {
new $className;
return $className;
catch (Error $error) {
return $defaultClassName;
}
The main disadvantage of this way is if my class with dynamically builded name exists, it will be constructed, but I don't need it right now, I need only class name if it exists

maxonfjvipon
Автор

the same could be achieved in php7x with a simple array using the $request->type as the key

MarkSnape
Автор

Please kindly make a video how to implement microservice architecture on Laravel Lumen API.

ArzamKhanYusufzai
Автор

Instead of casting the Model in string and can we use it like this?

$model = match($request->type){
'user' => \App\Models\User,
}
echo $model::all();
exit;

RahulKumar-rktf
Автор

Maybe an "if ternary" statement it could by even more shorter 🤔

TheMarcelitto
Автор

It may be better not to use "App \\ ..." It is better to use User :: class

KvinKinamoto
join shbcf.ru