如何在 Laravel 5 Eloquent 内显示文章标题的所有评论

How to show all comments in foreach with article title within Laravel 5 Eloquent

我正在关注教程文章和评论。 下面工作正常,但我如何在 foreach 中获取所有带有文章名称的评论?

型号:文章

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{  
public function comments() {
    return $this->hasMany('App\Http\Models\Comment');
}
protected $fillable = array('title','body');
}

型号:评论

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{  
public function article() {
    return $this->belongs('Article');
}
protected $fillable = array('body','article_id');


}

ArticleController 中的示例转储:

$items = Article::find(1)->comments()->get();
foreach ($items as $item) {
    print_r($item->body);
}

来自documentation关于查询关系

$article = Article::find(1);

echo $article->name;

foreach ($article->comments as $comment) {
    //
}

在我转储的 CommentController 中,例如:

foreach ( $comments as $comment) {
    echo $comment->body.' - '.$comment->article->title.'<br>';
}