5 months ago

retrieve all data from many to many relation

Posted 5 months ago by Rymercyble

hello, my database tables are db_articles, db_articles_files, db_files and keys are article_id and file_id


class StoredFiles extends Model
    protected $table = 'db_files';

    public function article(){
        return $this->belongsToMany('App\ArticleNew', 'db_articles_files', 'article_id', 'file_id', 'article_id', 'file_id');

class ArticleNew extends Model
    protected $table = 'db_articles_new';

    public function storedFiles(){
        return $this->belongsToMany('App\StoredFiles', 'db_articles_files', 'article_id', 'file_id', 'article_id', 'file_id');


ArticleNew::where('article_id', 125)->with('storedFiles')->get();

returns just 1 related file but there should be 5 files for this article

Please sign in or create an account to participate in this conversation.