I am working on a simple social network, i am working on the replies section now. I associated the post/status model with user as below, but when i tried to access in the vi
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();
}