I'm trying to pass an array from the laravel controller to my vue component, there is data already being passed successfully but i'm trying to send another one and this new one isn't working.
This is the controller function, if I dd($data); the correct data is shown.
$data['user'] = Auth::user()->load('userType');
$data['courses'] = Course::all();
$data['myClasses'] = StudentSchedule::where('instructor_id',Auth::user()->id)
->with('course', 'package.packageCourses', 'user', 'location')->get();
$data['locations'] = Location::all();
$data['notification'] = Notification::where('instructor_id', Auth::user()->id)
->with('student','instructor','location','course')->get();
if (Auth::user()->rankings()->count() > 0) {
$data['user']->ranking = Auth::user()->rankings()->avg('ranking');
} else {
$data['user']->ranking = 5;
}
return view('home', $data);
This is my blade
@extends('layouts.app')
@section('styles')
@parent
<link href="{{ asset('css/views/users/show.css') }}" rel="stylesheet">
@endsection
@section('content')
<user-show></user-show>
@endsection
@section('scripts')
<script>
const user = {!! json_encode($user) !!};
const courses = {!! json_encode($courses) !!};
const myClasses = {!! json_encode($myClasses) !!};
const locations = {!! json_encode($locations) !!};
</script>
<script src="{{ asset('js/users/show.js') }}"></script>
@endsection
This is the vue where I'm trying to get the data I set this is the data()
courses: [],
myClasses: [],
locations: [],
notification: [],
In the mounted
this.courses = courses;
this.myClasses = myClasses;
this.locations = locations;
this.notification = notification;
The errors I get are this, are on the this.myClasses = myClasses line in the mounted
Error in mounted hook: "ReferenceError: myClasses is not defined"
ReferenceError: myClasses is not defined
I don't understand why the myClasses doesn't work, while all the others do.
What am I doing wrong?
vue.jsafter initializing the variables in each view, I'd imagine there are a few cases where you're not likely initializing the same variables.const myClasses = {!! json_encode($myClasses) !!};ugh, working with this system is so confusing, I usually work with interia-vue that does all this for me without the need for blade or js files, only vue. This is an old project that I'm required to finish for work, thanks for your help!