Christian Gallarmin
Christian Gallarmin

Reputation: 660

Laravel Using Google Analytics - Use of undefined constant

Use of undefined constant analyticsData - assumed 'analyticsData' (this will throw an Error in a future version of PHP) (View: /home/vagrant/apps/rt2018/resources/views/records.blade.php) (View: /home/vagrant/apps/rt2018/resources/views/records.blade.php)"

My records.blade.php file

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello Analytics - A quickstart guide for JavaScript</title>
</head>
<body>

<h1>Hello Analytics</h1>

{{analyticsData}}

</body>
</html>

My Controller

use Spatie\Analytics\Period;
use Analytics;

public function showMonthlyReport($site_id, $report_id){

$reports = Report::where('report_id', $report_id)->firstOrFail();

$analyticsData = Analytics::fetchMostVisitedPages(Period::days(7));

return view('records', compact('site_id', 'report_id', 'reports', 'analyticsData'));

}

This is my blade file path to records.blade.php

<a href="{{route('Reports',['site_id'=>$report->site_id, 'report_id'=>$report->report_id])}}">view</a>

I tried to put any variable in my controller. For example $wow = 1; Add 'wow' on return view on my controller And put it to my blade {{wow}} is giving me the same error.

This is working before but I don't know what happen. Any {{object}} that put on my blade give me this error. Use of undefined constant (any declared object). When I tried to delete/comment out the analyticsData and also in my blade file it was working. It was working on Laravel 5.2 and PhP 7.2 before now I'm using Laravel 5.4. I know its not because the version. Do you have any idea how to fix this? Thank you in advance!

Upvotes: 1

Views: 927

Answers (1)

Tim Lewis
Tim Lewis

Reputation: 29278

Take the case when accessing analyticsData in .blade.php:

{{ analyticsData }}

Omitting the $ when attempting to access a variable in PHP attempts to access a constant with the same name. In this (and most cases), this is a typo, and the constant was never defined. The error message can be somewhat vague, but make sense when you understand what it's trying to do.

To fix this, simply reference the variable correctly, with the $ included:

{{ $analyticsData }}

Upvotes: 1

Related Questions