Laravel 在数据透视表中雄辩的 UUID

Laravel eloquent UUID in a pivot table(Laravel 在数据透视表中雄辩的 UUID)
本文介绍了Laravel 在数据透视表中雄辩的 UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

这个问题是这样的:laravel uuid 未显示在查询中.然而,这个问题的不同之处在于该表是一个带有 id 字段的数据透视表,使用通过 MySQL 触发器在插入时生成的 UUID.

This question is like this one: laravel uuid not showing in query. However, the difference in this question is about that table is a pivot table with id field uses UUID generated via MySQL trigger on insert.

我不想为该数据透视表创建另一个模型来为其提供类似问题的答案所考虑的解决方案.那么,有没有什么办法可以从与之相关的另一个模型中对数据透视表进行类型转换?

I don't want to create another model for that pivot table to supply it with the solution regarded on the similar question's answer. So, is there any way to perform type casting of the pivot table from another model related to it?

推荐答案

我想这可能是你想要的:

I think this might be what you want:

在定义 BelongsToMany 关系的模型中添加此属性:

In your model that defines the BelongsToMany relationship add this property:

protected $casts = ['relationName.pivot.id' => 'string'];

更新

我想我们可以在这里使用 php7.0 中的匿名类,而不是为枢轴创建模型类:

I guess we can make use of anonymous classes in php7.0 here instead of creating a model class for the pivot:

我没有测试这段代码,所以我不知道它是否有效,这只是一个想法

public function activeStatuses()
{
    return $this->belongsToMany('ModelName')
                ->using(class_basename(new class extends IlluminateDatabaseEloquentRelationsPivot {
                    protected $casts = ['id' => 'string'];
                }));
}

通常我更愿意为数据透视表创建模型或简单地使用数据透视类

Generally i would prefer to create model for the pivot table or simply use a pivot class

这篇关于Laravel 在数据透视表中雄辩的 UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

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 的问题)