我使用spatie / laravel-analytics当我在route.php上使用它时,一切正常,但是当我尝试在控制器中使用它时,它会抛出此错误!有点奇怪!这是我的控制器代码:
<?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'
));
}
}
我的route.php代码也是:
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;
});
先谢谢!
答案 0 :(得分:4)
在控制器上,您可以在命名空间行之后尝试:
use Analytics;
答案 1 :(得分:0)
在控制器上,使用以下命名空间:-{
use Illuminate\Support\Collection;
use Analytics;
use Spatie\Analytics\Period;