I'm using spatie/laravel-analytics everything is working fine when I'm using it on route.php but when I try to use it in a controller it throws this error! Kind of strange! This is my Controller Code:
<?php
namespace App\Http\Controllers;
use Spatie\Analytics\Period;
use Illuminate\Http\Request;
use App\Question;
use App\Contact;
use Carbon\Carbon;
class DashboardController extends Controller
{
public function index()
{
// $questions = Question::whereNull('answer')->get();
// $messages = Contact::where('status', false)->get();
//Data from Google Analytics
$totalVisitors = Analytics::fetchTotalVisitorsAndPageViews(Period::days(7));
// $mostVisitedPages = Analytics::fetchMostVisitedPages(Period::days(7), $maxResults = 5);
// $topReferrers = Analytics::fetchTopReferrers(Period::days(7), $maxResults = 5);
// $topBrowsers = Analytics::fetchTopBrowsers(Period::days(7), $maxResults = 5);
return $totalVisitors;
return view('admin.admin-dashboard', compact(
'messages', 'questions', 'totalVisitors', 'mostVisitedPages', 'topReferrers', 'topBrowsers'
));
}
}
Also my route.php code is :
use Spatie\Analytics\Period;
Route::get('/test', function() {
// Data from Google Analytics
$totalVisitors = Analytics::fetchTotalVisitorsAndPageViews(Period::days(7));
$mostVisitedPages = Analytics::fetchMostVisitedPages(Period::days(7), $maxResults = 5);
$topReferrers = Analytics::fetchTopReferrers(Period::days(7), $maxResults = 5);
$topBrowsers = Analytics::fetchTopBrowsers(Period::days(7), $maxResults = 5);
return $totalVisitors;
});
thanks in advanced !
On the controller you can try this after the namespace line:
use Analytics;
On the controller use these namespaces as below:-use Illuminate\Support\Collection; use Analytics; use Spatie\Analytics\Period;