Firstly, it is best instead of if($user->count()) {
to do if(!$user->count()) return;
to eliminate an indentation level.
Second, have you tried taking the forward slash off /public/img/avatars
?
Again, my mistake. I've changed my httpd.conf
file and I should save the uploaded images into img/avatars
, but what do you mean with if(!$user->count()) return;
. How should I implement it ?
beaverusiv proposes this syntax:
if(!$user->count()) return;
$user = $user->first();
$avatar = Input::file('avatar');
$avatarPath = '/public/img/avatars';
...
instead of:
if($user->count()) {
$user = $user->first();
$avatar = Input::file('avatar');
$avatarPath = '/public/img/avatars';
...
}
It's maybe not "better", I personally think it's more a matter of context and personal choices.
gilr is correct that it won't make or break your code, but to elaborate as to why I prefer it that way.
By making the exit condition or fail condition the one you're checking for you are removing a level of indentation from your code. This is because you don't need an else
clause as you are either creating an Exception, returning from the function or otherwise halting program execution.
Eliminating levels of indentation may sound trivial, but as your program becomes more featured you will find it easy to be at such great levels of indentation lines don't fit on the screen and you are so far into conditionals or anonymous functions that you can't keep track of what is actually happening.
Again, personal opinion, not necessary.
PS: If you've fixed your problem pick your answer to close the thread.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community