在为父关系选择特定列时访问嵌套关系

时间:2018-11-22 13:33:21

标签: laravel eloquent

在模型关系中,我想选择特定的列,但这使我失去了在该关系中调用其他嵌套关系的能力(例如ProductGroupPrices :: class)

如何为price()关系指定列并达到ProductGroupPrices :: class的内部关系,就像currency()

<?php

class ProductsGroups extends Model {   

    public function price(){
        return $this->hasOne(ProductGroupPrices::class, 'group_id')->select(['id', 'group_id', 'price'])->latest();
    } 
}

另一堂课

<?php

class ProductGroupPrices extends Model { 

    public function currency(){
        return $this->belongsTo(\App\Modules\Currencies\Models\Currencies::class, 'currency_id');
    }
}

1 个答案:

答案 0 :(得分:0)

选择所需的列:

public function price(){
    return $this->hasOne(ProductGroupPrices::class, 'group_id')
        ->select(['id', 'group_id', 'price', 'currency_id'])
        ->latest();                          ^^^^^^^^^^^^^
}