【问题标题】:change hasManyThrough() relation attribute name through accessor通过访问器更改 hasManyThrough() 关系属性名称
【发布时间】:2020-04-16 18:35:38
【问题描述】:

我有 3 个模型

  • 广告系列 PK(id)
  • CampaignMedium FK(campaign_id)
  • AccountReceivable FK(campaign_medium_id) (有金额列)

控制器功能:

public function all()
{
    return Campaign::with(['customer', 'receivedPayments'])->get();
}

在活动模型中,关系定义如下:

public function customer() 
{
    return $this->belongsTo(Customer::class);
}

public function accountReceivable()
{
    return $this->hasManyThrough(AccountReceivable::class, CampaignMedium::class);
}

public function receivedPayments()
{
    return $this->accountReceivable()
    ->selectRaw('sum(account_receivables.amount) as total')
    ->groupBy('campaign_id');
}

public function getReceivedPaymentsAttribute()
{
    if (!array_key_exists('receivedPayments', $this->relations)) {
        $this->load('receivedPayments');
    }

    $relation = $this->getRelation('receivedPayments')->first();

    return ($relation) ? $relation->total : 0;
}

最终输出:

{
"data": [
    {
        "id": 8,
        "name": "example",
        "image": "campaign/90375849f6c3cc6b0e542a0e3e6295b890375849f6c3cc6b0e542a0e3e6295b8.jpeg",
        "amount": 10,
        "description": "saddsa",
        "start_at": "2019-02-12 00:00:00",
        "end_at": "2019-02-12 00:00:00",
        "due_at": "2019-02-12 00:00:00",
        "status": "active",
        "customer": {
            "id": 1,
            "name": "test",
            "email": "info@test.com",
            "image": "customer/ec812116705ff3ae85298234fe6c4e97ec812116705ff3ae85298234fe6c4e97.jpeg",
            "address": "sample address"
        },
        "received_payments": [
            {
                "total": "700",
                "laravel_through_key": 8
            }
        ]
    },
    {
        "id": 9,
        "name": "example",
        "image": "campaign/fff9fadc92a809513dc28134379851aafff9fadc92a809513dc28134379851aa.jpeg",
        "amount": 10,
        "description": "saddsa",
        "start_at": "2019-02-12 00:00:00",
        "end_at": "2019-02-12 00:00:00",
        "due_at": "2019-02-12 00:00:00",
        "status": "active",
        "customer": {
            "id": 1,
            "name": "test",
            "email": "info@test.com",
            "image": "customer/ec812116705ff3ae85298234fe6c4e97ec812116705ff3ae85298234fe6c4e97.jpeg",
            "address": "sample address"
        },
        "received_payments": []
    }
]
}

summary:尝试获取 AccountReceivable 金额属性的总和,该属性工作正常,但 getReceivedPaymentsAttribute() 不工作,只需要返回总值。也有人可以帮我解释一下为什么 laravel_through_key 添加了 received_pa​​yments 吗?

【问题讨论】:

    标签: laravel eloquent orm has-many-through


    【解决方案1】:

    我从未尝试过以这种方式使用属性修饰符来修改关系。您正在覆盖 receivedPayments() 的预期结果。您最好像这样定义一个单独的属性:

    public function getSumReceivedPaymentsAttribute()
    {
        // ...your code... 
    }
    

    现在您可以使用 $model->sum_received_payments 访问属性或始终使用以下方法预加载它:

    // model.php
    protected $appends = ['sum_received_payments'];
    

    【讨论】:

    • 抛出异常:调用字符串上的成员函数 addEagerConstraints()
    • 它在哪里抛出了那个以及在哪个函数上?
    • 关于 getSumReceivedPaymentsAttribute() 函数
    • 您的属性函数正在生成以下查询:select sum(account_receivables.amount) as aggregate from account_receivables inner join campaign_mediums on campaign_mediums.id = account_receivables.campaign_medium_id 分组campaign_id
    • 生成前一个关系的位置:select sum(account_receivables.amount) as total, campaign_mediums.campaign_id as laravel_through_key from account_receivables inner join campaign_mediums on campaign_mediums.id = account_receivables.campaign_medium_id 其中campaign_mediums.campaign_id 在 (6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 , 19, 20, 21, 22) 分组campaign_id
    猜你喜欢
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多