Как показать комментарии с прокомментированным именем пользователя и фотографией - PullRequest
0 голосов
/ 22 ноября 2018

Я пытаюсь показать имя пользователя вместе с его комментарием, так как Tour не принадлежит пользователю, здесь я сталкиваюсь с проблемой [user].Не удалось передать информацию пользователя с комментариями.В моем коде я могу показывать только комментарии, которые относятся к туру, но не пользователи, которые комментируют.

Тур

class Tour extends Model{

protected $table = 'tour';

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

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

Модель TourReview

class TourReview extends Model{

protected $table = 'tour_review';

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

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

Пользователи Модель

class Users extends Model{

protected $table = 'users';

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

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

Контроллер

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

1 Ответ

0 голосов
/ 22 ноября 2018

Вы можете сделать вложенный запрос в контроллере

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
...