Laravel有许多模型与Publisher具有枢轴关系吗?

时间:2019-03-21 20:45:21

标签: mysql database laravel database-design eloquent

嗨,我在关系和数据库设计方面遇到了问题。

有三个表,分别称为“ articles”,“ users”和“ companies”,每个表的一个属性称为id:

"articles" -id

"users" -id

"companies" -id

article表包含许多用户模型或公司模型的发布者。

我的数据透视表必须类似于'authors'-id,-model_id,-model_name,-article_id

是否可以通过一个查询从一个集合中的模型中获取文章的发布者ID和数据?也许我误解了这个问题,有一种更简单的解决方案。

提前

3 个答案:

答案 0 :(得分:1)

假设Article只有一个公司或一个用户,正确的方法是建立一对多(多态)关系,以创建具有以下列的表: {article_id,articleable_id,articleable_type} 然后在您的模型中定义

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    /**
     * Get all of the owning articleable models.
     */
    public function articleable()
    {
        return $this->morphTo();
    }
}

和“用户”表:-

class Userextends Model
{
    /**
     * Get all of the user's articles.
     */
    public function articles()
    {
        return $this->morphMany('App\Article', 'articleable');
    }
}

与公司模式相同:-

class Company extends Model
{
    /**
     * Get all of the companies articles.
     */
    public function articles()
    {
        return $this->morphMany('App\Article', 'articleable');
    }
}

,然后您可以使用以下方式检索关系:-

$user = App\User::find(1);

foreach ($user->articles as $article) {
    // do this
}

here

但是,如果该文章可以包含多个用户或公司,则您必须执行here

的多对多态操作

答案 1 :(得分:0)

@Mohamed Gabr 问题是我需要很多方式。

我从用户模型或公司那里收到文章也没有问题。

我需要接收Article的作者。

我认为会有这样的事情。

class Article extends Model
{

    public function authors()
    {
        return $this->hasMany('App\Article_Authors','article_id');
    }
}

class Article_Authors extends Model
{

     public function authorable()
    {
        return $this->morphTo();
    }

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


class User extends Model
{

    public function authors()
    {
        return $this->morphMany('App\Article', 'authorable');
    }
}


class Company extends Model
{

    public function authors()
    {
        return $this->morphMany('App\Article', 'authorable');
    }
}

我可以使它工作,但我认为它不是很好的做法

答案 2 :(得分:0)

我实现了此模型

class Article extends Model
{

    public function authors()
    {
        return $this->hasMany('App\Article_Authors','article_id');
    }
}

class Article_Authors extends Model
{

     public function authorable()
    {
        return $this->morphTo();
    }

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


class User extends Model
{

    public function authors()
    {
        return $this->morphMany('App\Article', 'authorable');
    }
}


class Company extends Model
{

    public function authors()
    {
        return $this->morphMany('App\Article', 'authorable');
    }
}

在致电$ article->作者之后,我得到了

0   
id  1
record_id   2
authorable_id   1
authorable_type "App\\User"
created_at  "2019-03-22 15:56:38"
updated_at  "2019-03-22 15:56:38"
1   
id  2
record_id   2
authorable_id   1
authorable_type "App\\Company"
created_at  "2019-03-22 15:56:59"
updated_at  "2019-03-22 15:56:59"

现在如何调用此模型:(

@MohamedGabr

编辑: 经过研究后,我没有找到任何选择在一个查询中获得结果的选项,只有一个选项可以进行循环并检查模型的瞬间。 它会生成一个用于收集的战利品循环,我需要缓存结果,请暂时解决这个问题。

thx寻求帮助

相关问题