0
votes

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``
2

2 Answers

1
votes

There are some design flaws in what you have explained. You have a User model and a Prescription model. That means prescription_user is your pivot table (not junction table). If so far I'm correct, it means User and Prescription have a Many to many relationship. To prove my point, you said

Prescription_user table has a user_id and prescription_id.

That means prescription_user is the pivot table.

In your user model, define a many to many relationship with prescription. And vice versa.

User model

public function prescriptions() {
    return $this->belongsToMany(Prescription::class, 'prescription_user', 'user_id', 'prescription_id');
}

Then you can change your controller function like this

public function viewUserDetails()
{
    $user = Auth::user();
    $prescriptions = $user->prescriptions;

    return view('user/userdetails', compact('user', 'prescriptions'));
}

And your view

@extends('layouts.master')
@section('title', 'My Details')
@section('content')
    <h1>My Details </h1>

    <ul>
        <li>Name: {{ $user->firstname }} {{ $user->surname }}</li>
        <li>Email: {{ $user->email }}</li>
        <li>Address: {{ $user->address }}</li>
        <li>Postcode: {{ $user->postcode }}</li>
        <li>Date of Birth: {{ $user->dateofbirth }}</li>
        <li>Your Unique number is: {{ $user->uniquepatientnumber }}</li>
    </ul>

    Your are currently prescribed
    <ul>
        @foreach($prescriptions as $prescription)
            <li>{{ $prescription->name }}
        @endforeach
    </ul>

    <p>Note: If you believe any of these are incorrect please contact a receptionist</p>

@endsection

Your code would be neater and working great

Update

Getting data from the pivot table is relatively simple. Define those columns in the relationship

public function prescriptions() {
    return $this->belongsToMany(Prescription::class, 'prescription_user', 'user_id', 'prescription_id')->withPivot('column1', 'column2');
}

In your view, you display it like this

{{ $prescription->pivot->column1 }} {{ $prescription->pivot->column2 }}
0
votes

First I'm not sure that withData() is a thing. Just ->with($data).

Second, there is some confusion at the beginning of your function. You pass in a $userId, that you never use. Then you get the id from Auth::user(), then get a user with that id, which you already had. So just $user = Auth::user();

Third, you only get that error trying to use the -> operator on a non-object, and you only use it on $user-> and Auth::user()->

Since you got the user from Auth::user in the first place, I'm guessing it failed right out of the box at Auth::user()->id, and there is no Auth::user, but you would have to post a little more data, either from the error in the browser or from storage/logs/laravel.log.

BUT, if I use this code

Route::get('/', function () {
    $user = \Auth::user();
    die($user->name);
    return view('welcome');
}

where there clearly is no Auth::user() (I haven't even got to a login yet) I get this output:

ErrorException in web.php line 16:
Trying to get property of non-object
1. in web.php line 16
2. at HandleExceptions-> ... array('user'=>null)

So it's probaby that.