First, create a new PHP file in app\Helpers directory, for example UserServiceProvider.php.

<?php

use Illuminate\Support\Facades\Session;

function is_logged_in()
{
    if (Session::get("user_id") != "") {
        return true;
    }
    return false;
}

Next, create new providers using php artisan make:provider HelperServiceProvider in the console. Then update the file content as followed:

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        require base_path() . '/app/Helpers/UserServiceProvider.php';
    }
}

Lastly, add the reference to the helper provider in config\app.php, as followed:

'providers'=> [
    
    ...
    App\Providers\HelperServiceProvider::class,
    ...

]

Now, you can call the helper function anywhere by just calling the method is_logged_in().

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s