DEV Community

Cover image for Laravel Best Practice [Coding Standards Part 02] 🧑‍🦰👩‍🦰

Laravel Best Practice [Coding Standards Part 02] 🧑‍🦰👩‍🦰

Lathindu Pramduitha on April 19, 2021

Hello friends, This is the second article of Laravel coding standards article series. If you didn't see the first article, I recommend to read it...
Collapse
 
bravemaster619 profile image
bravemaster619

I'd like to add a thing to 02.

I've seen many devs use env function.

Please avoid env and use config instead.

Because .env files can be cached via php artisan config:cache and you can't use env function after that. (it will return null)

So if any of the code uses env function, you can't config cache in the future.

Collapse
 
lathindu1 profile image
Lathindu Pramduitha

great @bravemaster619 it's better to use config instep of env. and also we can use both.

we can use real content inside config file and. if we have local only thing we can use them inside .env .

like

"paypal_secret" = env("PAYPAL_SECRET" , 'asdfthdnejs323jn23nk3jn2njk3n2'),

and for our sandbox in local we can use key's inside .env

PAYPAL_SECRET = "kjansjnaknjsnalhjsbljhslhjlhjsla"

otherwise we don't want to use env anyware.

Collapse
 
bravemaster619 profile image
bravemaster619

This.

Collapse
 
ionbazan profile image
Ion Bazan

Using $request->all() is worst thing you can do. You should always use $request->validated().

Collapse
 
bravemaster619 profile image
bravemaster619

03 is more like: Avoid N+1 query problem.

Collapse
 
theowlsden profile image
Shaquil Maria

Thank you for this post!🙏
I am about to start developing a project in Laravel and these tips will come in handy!

Collapse
 
lathindu1 profile image
Lathindu Pramduitha

Thank you very much

Collapse
 
thuhtetdev profile image
Thu Htet Tun

Nice laravel tips, Lathindu.

Collapse
 
lathindu1 profile image
Lathindu Pramduitha

Thank you very much.

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great post! Well done! Awesome to see some high-quality Laravel content on here!

Collapse
 
sajaddp profile image
Sajad DP

Thanks man :)