简体   繁体   English

无法用雄辩的laravel 5显示上传文件

[英]Can't show upload file with eloquent laravel 5

I am bulid website that user can make a post and comment on it. 我是一个bulid网站,用户可以发帖并评论它。 The comment can have many file. 评论可以有很多文件。 Create comment and file work fine but when show the post, that just show post and comment, not with comment's file. 创建评论和文件工作正常,但在显示帖子时,只显示帖子和评论,而不是评论的文件。

//Post.php
public function commentsPost()
{
    return $this->hasMany('App\commentPost');
}

//CommentPost.php
public function post()
{
        return $this->belongsTo('App\Post');
}
public function filesPost()
{
    return $this->hasMany('App\FilePost');
}

//FilePost.php
public function commentsPost()
{
    return $this->belongsTo('App\CommentPost');
}

This my CommentController.php 这是我的CommentController.php

public function show($id)
{
    $post = Post::findOrFail($id);
    if ($post != null) {
        return view('post.show', compact('post'));

    } else {
        return redirect('/')->with('error', 'This post does not exist.');
    }
}

And show.blade.php show.blade.php

@foreach($post->commentsPost as $comment)   
  <h2>{{ $comment->message }} by {{ $comment->user->name }}</h2>
  @foreach($comment->filesPost->all() as $file)
    <h5>{{ $file->filename }}</h5>
  @endforeach
@endforeach

I use dd($post->commentsPost->filesPost->all()); 我用dd($post->commentsPost->filesPost->all()); and error Undefined property: Illuminate\\Database\\Eloquent\\Collection::$filesPost 和错误Undefined property: Illuminate\\Database\\Eloquent\\Collection::$filesPost

How to show the comment also the file? 如何显示评论文件? I use Laravel 5 我使用Laravel 5

try this 尝试这个

@foreach($post->commentsPost as $comment)   
  <h2>{{ $comment->message }} by {{ $comment->user->name }}</h2>
  @foreach($comment->filesPost() as $file)
    <h5>{{ $file->filename }}</h5>
  @endforeach
@endforeach

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM