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');
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
How to write this relationship in the model?
I don't know? please describe the relationship
@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؟
They look ok, provided roleusers table has a user_id column and the primary key of the users table is id
@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
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?
@Snapey
As follows
protected $table = 'user';
ok.
what line gives the error
@Snapey
error:
Trying to get property of non-object
line:
$find->user->name
@Snapey
problem :
in view
$find->user->name
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
u can use optional() helper
{{optional($find->user)->name}}
@rin4ik
error:
Trying to get property of non-object
@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);
}
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
@Snapey This is my table
user
id----name---username
role
id-----types
roleuser
id---user_id---role_id----active
Look, I'll put this as simply as I can
DO YOU HAVE ANY DATA IN THE DATABASE !!!
yes, I'm shouting
@Snapey
Yes - I have information in any database
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
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?
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.
Then why do you care about the user?
Wow ... this is like a skit on SNL.
Please or to participate in this conversation.