I'm trying to get data from a junction table and display it, so I have a users table and my junction table prescription_user. Prescription_user table has a user_id and prescription_id.
So what I'm trying to do is on this userdetails page, show all of this users prescriptions and access the prescription name and description etc, however when I try any of this I get "Trying to get property of non-object"
The models are set up to eager load correctly I think, the user model has a hasMany relation with the prescription_user model, and the prescription_user model has a belongsTo the user model.
Controller
function viewUserDetails($userId)
{
$logged_user_id = Auth::user()->id;
$user = User::find($logged_user_id);
$prescriptions = Auth::user()->prescription_user;
$fullname = $user->firstname ." ". $user->surname;
$email = $user->email;
$address = $user->address;
$postcode = $user->postcode;
$dob = $user->dateofbirth;
$uniquepatientnumber = $user->uniquepatientnumber;
$data = [];
$data['fullname'] = $fullname;
$data['email'] = $email;
$data['address'] = $address;
$data['postcode'] = $postcode;
$data['dateofbirth'] = $dob;
$data['uniquenumber'] = $uniquepatientnumber;
$data['prescriptions'] = $prescriptions;
return view('user/userdetails')->withData($data);`
userdetails blade
@extends('layouts.master')
@section('title', 'My Details')
@section('content')
<h1>My Details </h1>
<ul>
<li>Name: {{ $data['fullname'] }}</li>
<li>Email: {{ $data['email'] }}</li>
<li>Address: {{ $data['address'] }}</li>
<li>Postcode: {{ $data['postcode'] }}</li>
<li>Date of Birth: {{ $data['dateofbirth'] }}</li>
<li>Your Unique number is: {{ $data['uniquenumber'] }}</li>
<li>Your are currently prescribed {{$data['prescriptions']}}</li>
<p>Note: If you believe any of these are incorrect please contact a receptionist</p>
</ul>
@endsection``