首页 文章

Laravel orderBy有很多关系

提问于
浏览
2

我的下一个问题是订购具有hasMany Relationship的商品,订购字段在Relationship中 .

我有table:country,它只有ID字段我也有table:country_translation,它有列:name,lang

在我的CountryController中的index() - 方法我希望显示所有国家/地区的默认语言按参数名称对它们进行排序并对它们进行分页 .

这是我的国家模型:

class Country extends Model {

    protected $fillable = [
        'code', 'latitude', 'longitude', 'currency_id', 'timezone', 'dam_date', 'status',
    ];

    public function translations() {

        return $this->hasMany('App\Models\CountryTranslation');

    }
}

CountryTranslation模型:

class CountryTranslation extends Model {

    protected $fillable = [
        'name', 'lang', 'country_id',
    ];

    public function country() {
        return $this->hasOne('Country');
    }
}

1 回答

  • 1

    如果我正确地取消你,你应该这样做:

    $countries = CountryTranslation::where('lang', 'en')->orderBy('name')->paginate(20)
    

相关问题