如何投射 Eloquent Pivot 参数?

How To Cast Eloquent Pivot Parameters?(如何投射 Eloquent Pivot 参数?)
本文介绍了如何投射 Eloquent Pivot 参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有以下带有关系的 Eloquent 模型:

I have the following Eloquent Models with relationships:

class Lead extends Model 
{
    public function contacts() 
    {
        return $this->belongsToMany('AppContact')
                    ->withPivot('is_primary');
    }
}

class Contact extends Model 
{
    public function leads() 
    {
        return $this->belongsToMany('AppLead')
                    ->withPivot('is_primary');
    }
}

数据透视表包含一个附加参数 (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_primary 转换为布尔值?我已经尝试将它添加到两个模型的 $casts 数组中,但这并没有改变任何东西.

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.

推荐答案

由于这是数据透视表上的一个属性,因此使用 $casts 属性将不适用于 LeadContact 模型.

Since this is an attribute on the pivot table, using the $casts attribute won't work on either the Lead or Contact model.

但是,您可以尝试的一件事是使用自定义 Pivot 模型并定义了 $casts 属性.自定义数据透视模型的文档位于此处.基本上,您使用自定义创建一个新的 Pivot 模型,然后更新 LeadContact 模型以使用此自定义 Pivot 模型而不是基础模型.

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 new Pivot model with your customizations, and then update the Lead and the Contact models to use this custom Pivot model instead of the base one.

首先,创建您的自定义 Pivot 模型,它扩展了基本的 Pivot 模型:

First, create your custom Pivot model which extends the base Pivot model:

<?php namespace App;

use IlluminateDatabaseEloquentRelationsPivot;

class PrimaryPivot extends Pivot {
    protected $casts = ['is_primary' => 'boolean'];
}

现在,覆盖 LeadContact 模型上的 newPivot() 方法:

Now, override the newPivot() method on the Lead and the Contact models:

class Lead extends Model {
    public function newPivot(Model $parent, array $attributes, $table, $exists) {
        return new AppPrimaryPivot($parent, $attributes, $table, $exists);
    }
}

class Contact extends Model {
    public function newPivot(Model $parent, array $attributes, $table, $exists) {
        return new AppPrimaryPivot($parent, $attributes, $table, $exists);
    }
}

这篇关于如何投射 Eloquent Pivot 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)