How to fix "Non-static method Spatie\Analytics\Analytics::fetchVisitorsAndPageViews() should not be called statically?"

Viewed 1847

When i put:

use Spatie\Analytics\Analytics;

It gives the error

'Non-static method should not be called statically'

But when I only put:

use Analytics;

I gives a white page on refresh or says

"The use statement with non-compound name 'Analytics' has no effect "

when starting.

I am using Laravel 5.5.4 and although it says the facade should be automatically setup, it wasn't working so I also added this manually to the // config/app.php:

'Analytics' => Spatie\Analytics\AnalyticsFacade::class,

But it still is not working.

from the package github. there was a solution

php artisan config:clear

but it did not work for me.

3 Answers

This package can be installed through Composer.

composer require spatie/laravel-analytics

In Laravel 5.5 and above the package will autoregister the service provider. In Laravel 5.4 you must install this service provider.

config/app.php

'providers' => [
    ...
    Spatie\Analytics\AnalyticsServiceProvider::class,
    ...
];

In Laravel 5.5 and above the package will autoregister the facade. In Laravel 5.4 you must install the facade manually.

config/app.php

'aliases' => [
    ...
    'Analytics' => Spatie\Analytics\AnalyticsFacade::class,
    ...
];

You want to use the facade to access the class, you will need to change:

use Spatie\Analytics\Analytics; to use Analytics;

Another way around just import this to your class:

use Spatie\Analytics\AnalyticsFacade as Analytics

It depends in what context you place the use statement.

In Laravel you also can use facades without having to import it with use. The same class can be called by using \Analytics in your code call.

Example:

\Analytics::fetchMostVisitedPages(\Period::days(7));

Related