Laravel Associate method is returning empty array in the view

左心房为你撑大大i 提交于 2019-12-02 00:45:37

It seems like your are not attaching the replies to your $post variable while passing it to your view.
We have to use with() method to join these two tables.
Try, the following :

if(Auth::check()){ 
            $posts = Post::notReply()
                    ->where(function($query){ 
                        return $query->where('user_id', Auth::user()->id)
                                ->orWhereIn('user_id', Auth::user()->friends()->lists('id'));
                    })
                    ->with('replies')//Here we are joining the replies to the post
                    ->orderBy('created_at', 'desc')
                    ->get();
                    return view('timeline.index', compact('posts')); 
                }

Now, in your View you would need to use 2 foreach loop like below:

@foreach($posts as $post)
    {{--Your Code --}}
    @foreach($post->replies as $reply)
        {{--Your Code --}}
    @endforeach
    {{--Your Code --}}
@endforeach

Update:

Problem is with the postReply() method. In old code you were overriding the $post variable, due to which the reply was getting parent_id as its own id.
So, replace your old code with using $reply variable.
old:

$post= Post::create([
   'body' => $request->input("reply-{$statusId}"),
]);
$post->user()->associate(Auth::user());
$post->replies()->save($post);

new:

$reply = Post::create([
   'body' => $request->input("reply-{$statusId}"),
]);
$post->user()->associate(Auth::user());
$post->replies()->save($reply);

You are using $post for both the parent post and the reply, hence in

$post->replies()->save($post);

you are actually setting $post to be a reply to itself.

Would also be worth noting that column id is UNSIGNED but user_id and parent_id are not. I presume that there are foreign keys set on these?

Edit

Try this with a new $reply variable.

public function postReply(Request $request, $statusId){
    $this->validate($request, [
        "reply-{$statusId}" => 'required|max:1000',
    ],
    [
        'required' => 'The reply body is required'
    ]
    );

    $post = Post::notReply()->find($statusId);
    if(!$post){
        return redirect('/');
    }

    if(!Auth::user()->isFriendsWith($post->user) && Auth::user()->id !== $post->user->id){
        return redirect('/');
    }

    $reply = Post::create([
        'body' => $request->input("reply-{$statusId}"),
    ]);
    $reply->user()->associate(Auth::user());
    $post->replies()->save($reply);


    return redirect()->back();
}

You have setup your relations wrong. In your post model, instead of post having a self one-to-many relationship. It should be like this:

// App\User.php
public function replies()
{
    return $this->hasMany(App\Reply::class);
}

// App\Post.php
public function replies()
{
    return $this->hasMany(App\Reply::class);
}

// App\Reply.php
public function post()
{
    return $this->belongsTo(App\Post::class);
}

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

public function comments()
{
    return $this->belongsTo(App\Reply::class, 'parent_id');
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!