本文介绍了使用附件保存到数据透视表时,Laravel模型更改器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据透视表上有一个模型更改器,如下所示:

I have a model mutator on my pivot table like so:

当我这样保存时:

$account_transaction->subcategories()->attach($water_subcategory->id, ['amount'=>56]);

数据库显示56,而不是5600.

The database shows 56, instead of 5600.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class SubcategoryTransaction extends Model
{
protected $table = 'subcategory_transaction';
protected $fillable = ['amount'];

public function getAmountAttribute($value)
{
    if ($value) {
        $value = $value / 100;
        return $value;
    }
    return null;
}

public function setAmountAttribute($value)
{
    $value = $value * 100;
    dd($value);
    $this->attributes['amount'] = $value;
}
}

我能够使用一种在附加之前调用金额的方法来创建特征.

I was able to create a Trait with a method that gets called on the amount before attaching.

现在,当我像这样检索这些数据时:

Now when I retrieve these data like this:

 return $this_month_transactions = AccountTransaction::where('account_id', $account_id)
->whereBetween('date', [ $first_of_month_date->format('Y-m-d'), $last_of_month_date->format('Y-m-d'), ])
->with('entity','subcategories')
->get();

我需要对每个金额进行一轮($ value/100,2):

I need to run the round($value/100,2) on each amount:

"subcategories": [ { "id": 61, "once_monthly": 1, "transaction_category_id": 10, "name": "Rent & mortgage", "slug": "rent-mortgage", "type": "expense", "created_at": "2018-08-16 05:44:53", "updated_at": "2018-08-16 05:44:53", "pivot": { "transaction_id": 1, "subcategory_id": 61, "created_at": "2018-08-16 05:44:54", "updated_at": "2018-08-16 05:44:54", "amount": 72500 } }

"subcategories": [ { "id": 61, "once_monthly": 1, "transaction_category_id": 10, "name": "Rent & mortgage", "slug": "rent-mortgage", "type": "expense", "created_at": "2018-08-16 05:44:53", "updated_at": "2018-08-16 05:44:53", "pivot": { "transaction_id": 1, "subcategory_id": 61, "created_at": "2018-08-16 05:44:54", "updated_at": "2018-08-16 05:44:54", "amount": 72500 } }

我需要72500才能变成725.00

I need 72500 to become 725.00

推荐答案

只要您使用的是Laravel> = 5.5,就可以将访问器和变异器添加到数据透视模型中.

As long as you're using Laravel >=5.5 you can add accessor and mutators to a pivot model.

首先,更改您的SubcategoryTransaction类以扩展Pivot类而不是Model,因此您应该以类似以下内容结束

Firstly, change your SubcategoryTransaction class to extend the Pivot class instead of the Model so you should end up with something like:

use Illuminate\Database\Eloquent\Relations\Pivot;

class SubcategoryTransaction extends Pivot {

    /**
     * Convert the amount from pence to pounds.
     *
     * @param $amount
     * @return float|int
     */
    public function getAmountAttribute($amount)
    {
        return $amount / 100;
    }

    /**
     * Set the amount attribute to pence.
     *
     * @param $amount
     */
    public function setAmountAttribute($amount)
    {
        $this->attributes['amount'] = $amount * 100;
    }
}

然后在您的belongsToMany关系中链接另一个名为using()的方法,将其传递给您的数据透视模型名称,例如:

Then in your belongsToMany relationships chain another method on called using() passing it the name of your pivot model e.g.:

public function subcategories()
{
    return $this->belongsToMany(Subcategory::class)
        ->using(SubcategoryTransaction::class) // <-- this line
        ->withTimestamps()
        ->withPivot('amount');
}

这篇关于使用附件保存到数据透视表时,Laravel模型更改器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:49