数据透视表中的Laravel雄辩的UUID [英] Laravel eloquent UUID in a pivot table

查看:97
本文介绍了数据透视表中的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 \Illuminate\Database\Eloquent\Relations\Pivot {
                    protected $casts = ['id' => 'string'];
                }));
}

通常,我希望为数据透视表创建模型,或者只使用数据透视表

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆