How to retrieve user name from user table by using data (user_id) from another table having relationship
I have two models
User.php
public function opportunities()
{
return $this->hasMany('AppOpportunity');
}
Opportunity.php
public function user()
{
return $this->belongsTo('AppUser');
}
I have user_id
column in opportunities table and inserted user id (from user table using Auth) every time user posts record.
Now i need a view to return “this post is posted this user”.
First I find the post id by
$posts = Opportunity::find($id);
$posted_by = User::find($posts->user_id);
return view('opportunity.detail')->with('post', $posts, 'posted_by', $posted_by);
I have rendered user name by
But I got undefined constant in the view file $posted_by
while $post
is fine. Am I doing it in right way or not? I am passing two array variable to the post and its not working. Any help will be appreciated.
from Laravel Questions and Answers https://laravelquestions.com/laravel/how-to-retrieve-user-name-from-user-table-by-using-data-user_id-from-another-table-having-relationship/
via Lzo Media
No comments:
Post a Comment