php - Laravel Associate method is returning empty array in the view -


i working on simple social network, working on replies section now. associated post/status model user below, when tried access in view returns empty array [].

reply function in post model

public function postreply(request $request, $statusid){         $this->validate($request, [             "reply-{$statusid}" => 'required|max:1000',         ],         [             'required' => 'the reply body required'         ]         );          $post = post::notreply()->find($statusid);         if(!$post){             return redirect('/');         }          if(!auth::user()->isfriendswith($post->user) && auth::user()->id !== $post->user->id){             return redirect('/');         }          $post = post::create([             'body' => $request->input("reply-{$statusid}"),         ]);         $post->user()->associate(auth::user());         $post->replies()->save($post);           return redirect()->back();     } 

post model

<?php  namespace app;  use illuminate\database\eloquent\model;  class post extends model {     protected $table = 'posts';     protected $fillable = ['body'];     public function user(){         return $this->belongsto('app\user');     }      public function likes(){         return $this->hasmany('app\like');     }      public function scopenotreply($query){         return $query->wherenull('parent_id');     }      public function replies(){         return $this->hasmany('app\post', 'parent_id');     } } 

posts table

enter image description here

view accessing replies via $post->replies

<section class="row new-post">         <div class="col-md-6 col-md-offset-3">             <header><h3>what have say?</h3></header>             <form action="{{ url('createpost') }}" method="post">                 <div class="form-group">                     <textarea class="form-control" name="body" id="new-post" rows="5" placeholder="your post"></textarea>                 </div>                 <button type="submit" class="btn btn-primary">create post</button>                 <input type="hidden" value="{{ session::token() }}" name="_token">             </form>         </div>     </section>     <section class="row posts">         <div class="col-md-6 col-md-offset-3">             <header><h3>what other people say...</h3></header>                 @foreach($posts $post)                 <article class="post media" data-postid="{{ $post->id }}">                     <a class="pull-left" href="{{ url('user', $post->user->username) }}">                         <img class="media-object" alt="" src="{{ $post->user->getavatarurl() }}">                     </a>                     <div class="media-body">                       <h4 class="media-heading"><a href="{{ url('user', $post->user->username) }}"> {{ $post->user->username }}</a></h4>                         <p>{{ $post->body }}</p>                         <ul class="list-inline">                             <li>{{ $post->created_at->diffforhumans() }}</li>                             <li><a href="#">like</a></li>                             <li>10 likes</li>                         </ul>                         {{ $post->replies }}                      </div>                      <form role="form" action="{{ url('createpost',  $post->id )}}" method="post">                             <div class="form-group{{ $errors->has("reply-{$post->id}") ? ' has-error': '' }}">                                 <textarea name="reply-{{ $post->id }}" class="form-control" rows="2" placeholder="reply status"></textarea>                                 @if($errors->has("reply-{$post->id}"))                                     <span class="help-block">{{ $errors->first("reply-{$post->id}") }} </span>                                 @endif                             </div>                             <input type="submit" value="reply" class="btn btn-default btn-sm">                             <input type="hidden" name="_token" value="{{ session::token() }}">                     </form>                  </article>                  @endforeach          </div>  </section> 

ps : using 1 table relationship i.e posts , in posts table there column name parent_id via linking relationship table itself.

{{ $post->replies }} returns empty array. logic should return replies of comment related replies .

if thing else needed, mention share.

update : note replies user comment stored in database table posts unique id i.e parent_id thing when try access it returns empty array.

it seems not attaching replies $post variable while passing view.
have use with() method join these 2 tables.
try, 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 joining replies post                     ->orderby('created_at', 'desc')                     ->get();                     return view('timeline.index', compact('posts'));                  } 

now, in view need use 2 foreach loop below:

@foreach($posts $post)     {{--your code --}}     @foreach($post->replies $reply)         {{--your code --}}     @endforeach     {{--your code --}} @endforeach 

update:

problem postreply() method. in old code overriding $post variable, due reply getting parent_id own id.
so, replace old code 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); 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -