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

arshver's avatar

Trying to get property of non object

controller:

  public function edit($id){
        $find=Roleuser::find($id);
        return view('admin.role.roleupdate',compact('find'));
    }

view:

$dd=$find->user_id;
  $pos=DB::table('users')->where('id','=',$dd)->pluck('name');
0 likes
29 replies
Snapey's avatar

Why are you trying to do a DB query in the view?

It could work, but its bad practice.

Assuming you have a relationship between Roleuser model and User model then you should eager load in the controller;

  public function edit($id){
        $find=Roleuser::with('user')->find($id);
        return view('admin.role.roleupdate',compact('find'));
    }

and then in the view, you can use {{ $find->user->name }}

Snapey's avatar

I don't know? please describe the relationship

arshver's avatar

@Snapey I wrote the following model user

public function roleuser()
    {
        return $this->hasOne(Roleuser::class);
    }

model roleuser

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

it's true؟

Snapey's avatar

They look ok, provided roleusers table has a user_id column and the primary key of the users table is id

arshver's avatar

@Snapey This is my table

user

id----name---username

role

id-----types

roleuser

id---user_id---role_id----active

The error below

Trying to get property of non object
Snapey's avatar

I assume in your models then you have needed to specify the table name since by convention they should be plural (users, roles, roleusers)

What line gives the error?

arshver's avatar

@Snapey error:

Trying to get property of non-object 

line:

$find->user->name
Snapey's avatar

So the error is saying that this Roleuser does not have a user

Is your data setup ok with the correct relationships?

Try this;

  public function edit($id){
        $find=Roleuser::with('user')->find($id);

    dd($find);

        return view('admin.role.roleupdate',compact('find'));
    }

When the dump appears, expand the relations section and check that it contains the user

rin4ik's avatar

u can use optional() helper

{{optional($find->user)->name}}
arshver's avatar

@Snapey I wrote the following model user

public function roleuser()
    {
        return $this->hasOne(Roleuser::class);
    }

model roleuser

    public function user()
    {
        return $this->belongsTo(User::class);
    }
Snapey's avatar

You said already... What has that got to do with it?

It does not tell me that you actually have related models in the database tables.

ie, user with id=1 and roleuser with user_id=1

arshver's avatar

@Snapey This is my table

user

id----name---username

role

id-----types

roleuser

id---user_id---role_id----active
Snapey's avatar

Look, I'll put this as simply as I can

DO YOU HAVE ANY DATA IN THE DATABASE !!!

yes, I'm shouting

Snapey's avatar

Lets loop back so that you get chance to read it again...

So the error is saying that this Roleuser does not have a user

Is your data setup ok with the correct relationships?

Try this;

  public function edit($id){
        $find=Roleuser::with('user')->find($id);

    dd($find);

        return view('admin.role.roleupdate',compact('find'));
    }

When the dump appears, expand the relations section and check that it contains the user

Snapey's avatar

So you cannot find the Roleuser with the $id ?What id was passed into edit? Do you have a row in the roleuser table with the id that was passed?

Cronix's avatar

Are you trying to edit the user, or the role?

public function edit($id){
        $find=Roleuser::find($id);

That's getting the role by the $id passed in... not the user.

Cronix's avatar

Then why do you care about the user?

Please or to participate in this conversation.