如何铸就雄辩的枢轴参数? [英] How To Cast Eloquent Pivot Parameters?
问题描述
我有以下具有关系的雄辩模型:
class Lead extends Model {
public function contacts() {
return $ this-> belongsToMany('App \Contact')
- > withPivot('is_primary');
}
}
class Contact扩展模型{
public function leads(){
return $ this-> belongsToMany('App'\\Lead ')
- > withPivot('is_primary');
}
}
数据透视表包含一个额外的参数( is_primary
)标记一个关系作为主要的。目前,当我查询联系人时,我会看到这样的回报:
{
pre>
id:565,
lead:[
{
id:349,
pivot:{
contact_id:565,
lead_id :349,
is_primary:0
}
}
]
}
有没有办法将
is_primary
转换为布尔值?我已经尝试将它添加到两个模型的$ casts
数组中,但没有改变任何东西。解决方案由于这是数据透视表上的属性,因此使用
$ casts
属性将无法在领导
或联系
模型。
但是,有一件事可以尝试使用定义的
$ casts
属性定制数据透视
模型。关于自定义枢纽模型的文档是 here 。基本上,您可以使用自定义创建一个新的数据透视表 c> c,然后更新
Lead
和联系
模型以使用此自定义数据透视
模型而不是基础模型。
首先,创建您的自定义
Pivot
模型,扩展基础数据透视
模型:<?php命名空间应用程序;
使用Illuminate\Database\Eloquent\Relations\Pivot;
class PrimaryPivot extends Pivot {
protected $ casts = ['is_primary'=> 布尔];
}
现在,覆盖
newPivot()$
Lead
和联系人
模型中的c $ c>方法:class Lead extends Model {
public function newPivot(Model $ parent,array $ attributes,$ table,$ exists){
return new \App \PrimaryPivot($ parent,$ attributes,$ table,$ exists);
}
}
class Contact扩展模型{
public function newPivot(Model $ parent,array $ attributes,$ table,$ exists){
返回新的\App\PrimaryPivot($ parent,$ attributes,$ table,$ exists);
}
}
I have the following Eloquent Models with relationships:
class Lead extends Model { public function contacts() { return $this->belongsToMany('App\Contact') ->withPivot('is_primary'); } } class Contact extends Model { public function leads() { return $this->belongsToMany('App\Lead') ->withPivot('is_primary'); } }
The pivot table contains an additional param (
is_primary
) that marks a relationship as the primary. Currently, I see returns like this when I query for a contact:{ "id": 565, "leads": [ { "id": 349, "pivot": { "contact_id": "565", "lead_id": "349", "is_primary": "0" } } ] }
Is there a way to cast the
is_primary
in that to a boolean? I've tried adding it to the$casts
array of both models but that did not change anything.解决方案Since this is an attribute on the pivot table, using the
$casts
attribute won't work on either theLead
orContact
model.One thing you can try, however, is to use a custom
Pivot
model with the$casts
attribute defined. Documentation on custom pivot models is here. Basically, you create a newPivot
model with your customizations, and then update theLead
and theContact
models to use this customPivot
model instead of the base one.First, create your custom
Pivot
model which extends the basePivot
model:<?php namespace App; use Illuminate\Database\Eloquent\Relations\Pivot; class PrimaryPivot extends Pivot { protected $casts = ['is_primary' => 'boolean']; }
Now, override the
newPivot()
method on theLead
and theContact
models:class Lead extends Model { public function newPivot(Model $parent, array $attributes, $table, $exists) { return new \App\PrimaryPivot($parent, $attributes, $table, $exists); } } class Contact extends Model { public function newPivot(Model $parent, array $attributes, $table, $exists) { return new \App\PrimaryPivot($parent, $attributes, $table, $exists); } }
这篇关于如何铸就雄辩的枢轴参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!