如何显示带有评论用户名和照片的评论

How to show comments with commented user name and photo

我正在尝试显示用户的姓名及其评论,因为 Tour 不属于用户,我在这里遇到 [用户] 问题。无法传递带评论的用户信息。在我的代码中,我只能显示属于旅游的评论,但不能显示评论的用户。

Tour

class Tour extends Model{

protected $table = 'tour';

public function disTour()
{
    return $this->hasMany('App\District');
}

public function review()
{
    return $this->hasMany(TourReview::class);
}

TourReview Model

class TourReview extends Model{

protected $table = 'tour_review';

public function tour()
{
    return $this->belongsTo('App\Tour');
}

public function user()
{
    return $this->belongsTo('App\Users');
}

Users Model

class Users extends Model{

protected $table = 'users';

public function userBlogs()
{
    return $this->hasMany('App\Blog');
}

public function tourReview()
{
    return $this->hasMany('App\TourReview');
}

Controller

public function singleDetails($id)
{
    $tour = Tour::find($id);

    $comments = Tour::find($id)->review;
    $users = TourReview::with('user')->where('id', $comments->pluck('id'))->get();
    foreach ($users as $user){
        dd($user);
    }
    //$blogs = Blog::with('images')->where('user_id', $user_id)->paginate(10);
    dd($comments);
    return view('Tours.single_tour')
        ->with(compact('tour', 'comments'));
}

Blade View

@foreach($comments as $comment)
    <div class="review_strip_single">
        <img src="{{asset('wanna show commented user photo')}}" height="78" width="78" alt="Image"  class="img-circle">
         <small> - {{$comment->created_at->format('d M Y')}} -</small>
         <h4>{{wanna show user name}}</h4>
            <p>  {{$comment->tourreview_desc}}    </p>
     </div>
@endforeach

you can do nested query in controller

public function singleDetails($id)
{
    $tour = Tour::with(['review.user'])->find($id);

    return view('Tours.single_tour')
        ->with(compact('tour'));

或者如果您只想要评论

$comments = Review::with('user')->whereHas('tour', function ($q)use($id){
             $q->where('id', $id);
     });

    return view('Tours.single_tour')
        ->with(compact('comments'));
}

Blade View

 @foreach($tour->comments as $comment)
         <div class="review_strip_single">
             <img src="{{asset('wanna show commented user photo')}}" height="78" width="78" alt="Image"  class="img-circle">
                 <small> - {{$comment->created_at->format('d M Y')}} -</small>
                 <h4>{{$comment->user->name}}</h4>
                   <p>  {{$comment->tourreview_desc}}  </p>
         </div>
   @endforeach

 @foreach($comments as $comment)
     <div class="review_strip_single">
         <img src="{{asset('wanna show commented user photo')}}" height="78" width="78" alt="Image"  class="img-circle">
            <small> - {{$comment->created_at->format('d M Y')}} -</small>
            <h4>{{$comment->user->name}}</h4>
            <p>  {{$comment->tourreview_desc}}  </p>
     </div>
   @endforeach