繁体   English   中英

如何在Laravel中从HybridRelations选择特定字段?

[英]How to select specifics fields form HybridRelations in laravel?

嗨,我有一个带有后端连接的模型“ Trip”,另一个带有mongodb连接的模型“ ComponentValue”

我创建了一个查询来检索具有这样的组件值的行程

public function getVehicleTrips($vehicleID, $filters = null, $withPaginate = true)
{
    try {

        $query = Trip::query()
            ->where('vehicle_id', '=', $vehicleID)
            ->with('heightsDamagedComponentValue')
            ->with('lowestDamagedComponentValue')
            ->with('heightsDamagedComponentValue.componentType')
            ->with('lowestDamagedComponentValue.componentType');


        $query = $this->applyTripsDatesFilters($query, $filters);



        if ($withPaginate) {
            $query = $query->paginate(Helpers::getKeyValue($filters, 'limit', 10));

        }

        $query = $query->sortByDesc('heightsDamagedComponentValue.damage')->values();

        $result = $query;

        return $result;

    } catch (\Exception $e) {

        return false;
    }
}

检索了数据,但是heightsDamagedComponentValue有一些文件,我不想将它们包括在结果中,甚至我也不想将其包括在查询选择中

那么我如何才能从mongo关系heightsDamagedComponentValue中检索某些字段呢?

我已经尝试过此解决方案并尝试添加

 protected $guarded = ['id', 'data'];

ComponentValue模型,但它也不起作用,

heightsDamagedComponentValue方法是

public function heightsDamagedComponentValue()
{
    return $this->hasOne(ComponentValue::class)->orderBy('damage', 'desc');
}

请任何帮助,并在此先感谢

这很简单,只需将选择添加到关联方法中,即可选择任何类似的文件

public function heightsDamagedComponentValue()
{
    return $this->hasOne(ComponentValue::class)
        ->select([
            'trip_id',
            'vehicle_id',
            'component_type_id',
            'damage'
        ])
        ->orderBy('damage', 'desc');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM