SaeedPrez
452
15
Tips

Use custom helper functions for +readability

Posted 2 years ago by SaeedPrez

Hey guys,

Just a little tip/reminder to use custom helpers to make your code more readable..

Let's take the simple auth()->check() as an example. Instead of this:

@if(auth()->check())
    <!-- Authenticated User Navbar -->
@endif

@if(auth()->check() && auth->user()->admin == 1)
    <!-- Admin Navbar -->
@endif

..you could do this..

@if(userIsLoggedIn())
    <!-- Authenticated User Navbar -->
@endif

@if(userIsAdmin())
    <!-- Admin Navbar -->
@endif

Your imagination sets the limit. Creating a custom helper takes a few seconds and can make your code much more readable and perhaps even save time.

/**
 * Check if user is logged in
 *
 * @return boolean
 */
function userIsLoggedIn()
{
    return auth()->check();
}

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.