【问题标题】:How to define the Many-to-Many relationship of inner-package model and outer-package model in LaravelLaravel中如何定义内包模型和外包模型的多对多关系
【发布时间】:2018-03-27 15:40:40
【问题描述】:

如何定义内包模型和外包模型的关系?

例如: 我在创建 Laravel 包名 ArticleManager 时,会提供一些博客文章 CRUD 的功能。

使用我们的 ArticleManager 包的一些项目会在 User 和 Article 之间建立多对多的关系。在我们的包中,一个特征可能对用户模型有用:

  public function articles()
    {
        return $this->belongsToMany('XXX/Article');
    }

但是如何或在哪里声明“Articles belongsToMany Users”? 我们无法在包中的 Article Model 中编写这样的代码。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    你可以使用配置文件:

    public function users()
    {
        return $this->belongsToMany(config('article_manager.users.model'));
    }
    

    配置文件 config/article_manager 如下:

    {
         users:
         {
              model: 'App\User'
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-10
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 2014-02-02
      • 2018-06-15
      • 2022-01-12
      • 2018-12-12
      • 2017-05-25
      相关资源
      最近更新 更多