Php 使用“附加”保存数据透视表时,Laravel模型转换器不工作
我的数据透视表上有一个模型变量,如下所示: 当我这样保存到它时:Php 使用“附加”保存数据透视表时,Laravel模型转换器不工作,php,laravel,orm,model,eloquent,Php,Laravel,Orm,Model,Eloquent,我的数据透视表上有一个模型变量,如下所示: 当我这样保存到它时: $account_transaction->subcategories()->attach($water_subcategory->id, ['amount'=>56]); 数据库显示的是56,而不是5600 <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class SubcategoryTransact
$account_transaction->subcategories()->attach($water_subcategory->id, ['amount'=>56]);
数据库显示的是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;
}
}
我需要对每个金额进行一轮($value/100,2):
“子类别”:[
{
“id”:61,
“每月一次”:1,
“交易类别id”:10,
“名称”:“租金和抵押”,
“slug”:“租金抵押”,
“类型”:“费用”,
“创建时间”:“2018-08-16 05:44:53”,
“更新时间”:“2018-08-16 05:44:53”,
“支点”:{
“交易id”:1,
“子类别_id”:61,
“创建时间”:“2018-08-16 05:44:54”,
“更新时间”:“2018-08-16 05:44:54”,
“金额”:72500
}
}
我需要72500变成725.00只要您使用的是Laravel>=5.5,您就可以向枢轴模型添加访问器和变异器 首先,更改您的
子类别事务
类以扩展透视
类,而不是模型
,因此您应该得到如下结果:
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;
}
}
然后,在您的belongToMany
关系中,使用()链接另一个名为的方法,将枢轴模型的名称传递给它,例如:
public function subcategories()
{
return $this->belongsToMany(Subcategory::class)
->using(SubcategoryTransaction::class) // <-- this line
->withTimestamps()
->withPivot('amount');
}
公共功能子类别()
{
返回$this->belongstomy(子类别::类)
->使用(子类别事务::类)//withTimestamps()
->withPivot(“金额”);
}
只要您使用的是Laravel>=5.5,就可以向枢轴模型添加访问器和变异器
首先,更改您的子类别事务
类以扩展透视
类,而不是模型
,因此您应该得到如下结果:
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;
}
}
然后,在您的belongToMany
关系中,使用()链接另一个名为的方法,将枢轴模型的名称传递给它,例如:
public function subcategories()
{
return $this->belongsToMany(Subcategory::class)
->using(SubcategoryTransaction::class) // <-- this line
->withTimestamps()
->withPivot('amount');
}
公共功能子类别()
{
返回$this->belongstomy(子类别::类)
->使用(子类别事务::类)//withTimestamps()
->withPivot(“金额”);
}
看起来您的getter和setter正在转换为其原始值,即56。setter正在乘法,getter正在占卜,因此返回原始值。这是一个想法,但它应该在保存之前命中setter,DB应该显示值*100。出于某种原因,它没有命中setter。是的,它没有被命中。没有发生任何意外pens。您正在尝试对将保存到透视表的值进行变异。但变异器仅适用于子类别事务模型,而不适用于透视table@AfrazAhmad仅供参考,从5.5开始,您现在可以在数据透视中使用访问器和变异器。看起来您的getter和setter正在转换为其原始值,即56。setter正在相乘getter正在占卜,因此返回原始值。这是一个想法,但它应该在保存之前命中setter,DB应该显示值*100。出于某种原因,它没有命中setter。是的,它没有命中。什么都没有发生。您正在尝试对将保存到pivot表的值进行变异。但mutator只对Sub起作用categoryTransaction模型,不适用于pivottable@AfrazAhmad仅供参考,从5.5开始,你现在可以在Pivot中使用访问器和变异器。你就是Ross!我还需要在子类别()上使用Pivot->方法?@0和1是的,你会的。很抱歉,我没有将其添加到示例中。你就是罗斯!我是否仍然需要子类别()方法上的->withPivot
?@0和1是的,你会的。很抱歉,我没有将其添加到示例中。