Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andy7's avatar
Level 1

Property does not exist on this collection instance.

I'm having a bit of a problem with calling the values from the table of a user when im trying to edit that user through another table and im getting the error that the property does not exist I tried to add the belongsTo on the model but its still not working

Heres the controller

public function edit($id)
    {
        $teams =Team::all();
        $departments =Department::all();
        $leaveEntitlements=LeaveEntitlement::find($id);
        return view('manageleave.index', compact('departments','leaveEntitlements','user'));

    }
    public function update(Request $request, $id)
    {
        $leaveEntitlements = LeaveEntitlement::find($id);
        $leaveEntitlements->update($request->all());
        return redirect()->route('manageleave.index')->with('message', 'LeaveEntitlement updated');
    }

And heres the model

public function user(){
        return $this->belongsTo(User::class,'user_id','id');
    }

And im using it here but i want to add more to it inside the forms like other columns data but it catches an error here first

<form method="POST" action="{{route('manageleaves.update',[$leaveEntitlements->user->id])}}">

The error is this

Property [user] does not exist on this collection instance. 
0 likes
11 replies
Sinnbeck's avatar

Are you setting $leaveEntitlements anywhere else? It seems you are loading multiple some place (->get()) and trying to get user on that.

andy7's avatar
Level 1

Accidentally clicked on the answer can i return it somehow? Also yes im calling that on the index function too

Sinnbeck's avatar

To remove best answer make a dummy answer, mark it as best, delete it.

andy7's avatar
Level 1

Thanks and the reason why im using it both on index is because the edit is on a modal how would i go to make this work i would quite like it keep it in modal but if its not possible i can move it

frankielee's avatar

You are missing the variable user in the edit method?

public function edit($id)
    {
        $teams =Team::all();
        $departments =Department::all();
        $leaveEntitlements=LeaveEntitlement::find($id);
        return view('manageleave.index', compact('departments','leaveEntitlements','user'));<====

    }

Sinnbeck's avatar

Search though your code and find all places where you set $leaveEntitlements. I am sure that somewhere you are using something else than find()

andy7's avatar
Level 1

User find id correct? I had that commented so i just deleted it from here because i was testing

andy7's avatar
Level 1

I use the ::all() on the index function and ::find() on the update there is this the problem? and if it is how can i still use them both

Sinnbeck's avatar

Please show the index method, and how you use it in the blade file for the index.

Most likely you need to update the code in the blade file for index to something like

@foreach($leaveEntitlements as $leave)
    $leave->user->name;
@endforeach
andy7's avatar
Level 1

This is the index function

public function index()
    {
        //
        $users = User::all();
        $roles= Role::all();
        $jobs=Job::all();
        $departments=Department::all();
        $teams=Team::all();
        $leaveEntitlements=LeaveEntitlement::all();


        return view('/admin/manageleaves/index',compact('users' ,'roles','jobs','departments' ,'teams','leaveEntitlements'));


    }
andy7's avatar
andy7
OP
Best Answer
Level 1

Okay so i figured it out , changed the relation for it

public function entitlement(){
        return $this->hasOne(LeaveEntitlement::class,'user_id','id');
    }

And its working now

Please or to participate in this conversation.