mardi 30 janvier 2018

Get all related articles from given article using eager loading

I want to get all related articles of particular article based on category.

I have relation between Article and Category

Article.php

public function category(){

    return $this->belongsTo(ArticleCategory::class,'category_id','id');

}

ArticleCategory.php

 public function articles(){

    return $this->hasMany(Article::class,'category_id','id');

}

ArticleController.php

  public function singleArticle(Article $article){

// I want to convert this statement to eager loading statement

    $relatedArticles = $article->category->articles;

    return view('pages/article',compact('article','relatedArticles'));


}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire