Support the ongoing development of Laravel.io →
posted 10 years ago
Testing
Last updated 2 years ago.
0

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?

Last updated 2 years ago.
0

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 ?

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

SzkolaWEB szkolaweb Joined 26 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.