I've created a helper file in App
folder named as Helper.php
.
app/Helper.php
<?php
namespace App;
use Illuminate\Support\Facades\DB;
class Helper {
public function get_username($user_id)
{
$user = DB::table('users')->where('userid', $user_id)->first();
return (isset($user->username) ? $user->username : '');
}
}
app/Providers/HelperServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider
{
public function boot()
{
//
}
public function register()
{
require_once app_path() . 'Helper.php';
}
}
config/app.php
Inside the provider's array...
App\Providers\HelperServiceProvider::class,
Inside aliases
array...
'Helper' => App\Helper::class,
Everything was working fine but now I have the following error.
ErrorException thrown with message "Non-static method Helper::get_username($user->id) should not be called statically
But when I add static
keyword to function its works fine. What's the difference between static and non-static methods?