简体   繁体   English

使用附件保存到数据透视表时,Laravel模型更改器不起作用

[英]Laravel model mutator not working when using attach to save on pivot table

I have a model mutator on my pivot table like so: 我在数据透视表上有一个模型更改器,如下所示:

When I save to it like this: 当我这样保存时:

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

The database shows 56, instead of 5600. 数据库显示56,而不是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();

I need to run the round($value/100,2) on each amount: 我需要在每个金额上运行回合($ value / 100,2):

"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 } }

I need 72500 to become 725.00 我需要72500才能变成725.00

As long as you're using Laravel >=5.5 you can add accessor and mutators to a pivot model. 只要您使用Laravel> = 5.5,就可以将访问器和更改器添加到数据透视模型中。

Firstly, change your SubcategoryTransaction class to extend the Pivot class instead of the Model so you should end up with something like: 首先,更改您的SubcategoryTransaction类以扩展Pivot类而不是Model因此您应该得到如下结果:

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;
    }
}

Then in your belongsToMany relationships chain another method on called using() passing it the name of your pivot model eg: 然后在您的belongsToMany关系链中,另一个名为using()将其传递给您的数据透视模型名称,例如:

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

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

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