1
votes

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!

1
{{analyticsData}} should be {{ $analyticsData }}, although it might not output correctly; but that's a different issue.Tim Lewis
@TimLewis same issue.Christian Gallarmin
That doesn't make sense. I just tried that in my Laravel 5.4 application; {{ products }} - Undefined Constant, {{ $products }} - [] (is an empty array). You did add the $ right? I didn't just adjust the spacing (as spacing doesn't matter, just for readability).Tim Lewis
@TimLewis I tried that both but i got the same error. It was working before but i don't know what happen. Like my {{ anything }} is broken or something that I don't know how to fix it.Christian Gallarmin
And you're adjusting the correct .blade.php file? I don't know of any instance where {{ anything }} would work without you defining anything as a global constant; you're not referencing with $, so you should (and are) getting Undefined constant ...Tim Lewis

1 Answers

1
votes

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 }}