【问题标题】:Laravel getting empty queryLaravel 得到空查询
【发布时间】:2016-07-07 07:10:08
【问题描述】:

我有一个名为 Region 的模型,它可以包含多个国家/地区。我想得到它们,但是当我使用查询输出查看为什么它没有找到任何东西时,我得到一个空查询 []。

这是我的 Region 方法:

public function countries() {

    return $this->hasMany('App\Models\Country', 'country_region_relation', 'country_id', 'region_id');
}

【问题讨论】:

    标签: php laravel relationship laravel-5.2


    【解决方案1】:

    改成

    public function countries() {
       return $this->hasMany('App\Models\Country');
    }
    

    我认为您的方案是多对多关系。所以如果上述方法不起作用,试试这个

    public function countries() {
       return $this->belongsToMany('App\Models\Country', 'country_region_relation', 'country_id', 'region_id');
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-28
      • 2014-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-30
      • 2021-09-09
      • 2020-03-25
      • 1970-01-01
      相关资源
      最近更新 更多