php 在控制器上使用时找不到类别'App\Http\Controllers\Analytics'

cnwbcb6i  于 2022-11-21  发布在  PHP
关注(0)|答案(2)|浏览(120)

我用的是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;

});

=

bvhaajcl

bvhaajcl1#

在控制器上,您可以在命名空间行之后尝试此操作:

use Analytics;
8hhllhi2

8hhllhi22#

在控制器上使用以下名称空间:-use Illuminate\Support\Collection; use Analytics; use Spatie\Analytics\Period;

相关问题