我有一个用户表。用户可以上传帖子。该帖子很好地保存到数据库中。用户可以相互关注 - 所以在数据透视表上,每个follower_id都有followees_id。访问用户的属性(使用数据透视表)laravel 5.1
我需要获取当前用户followee的帖子。我有点尴尬从数据透视表中获取它。
这里是我到目前为止的代码:
控制器:
protected function get_followee_posts($followee_posts) ////$followee_posts passed from the route.
{
$user = $this->user;
$user->followee()->attach($followee_posts);
$followee_posts = User::find($followee_posts);
}
观点:
<div class="following_posts">
<p> Posts from people you're following: <p>
@foreach ($user->follower_id->followee_id->posts as $post)
<form action="/html/tags/html_form_tag_action.cfm" method="post">
<textarea name="comments" id="comments" style="width:96%;height:90px;background-color:white;color:black;border:none;padding:2%;font:22px/30px sans-serif;">
{!! $posts->full_post !!} </textarea>
</form>
@endforeach
路线:
Route::get('hub/{followee_posts}','[email protected]_followee_posts')->name('followee_posts');
我得到一个错误与当前的代码说:
ErrorException in 7340f90cc5faf1a298fcc646b7248b22 line 105:
Trying to get property of non-object
任何帮助将是可爱的。谢谢。
我觉得你使用'附加()'如果不当你的意图是简单地从数据库中检索的记录。 'attach()'用来在一个多对多关系的中间表中插入一条记录。 – Jeemusu