問題描述
這個問題是這樣的:laravel uuid 未顯示在查詢中.然而,這個問題的不同之處在于該表是一個帶有 id
字段的數(shù)據(jù)透視表,使用通過 MySQL 觸發(fā)器在插入時生成的 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.
我不想為該數(shù)據(jù)透視表創(chuàng)建另一個模型來為其提供類似問題的答案所考慮的解決方案.那么,有沒有什么辦法可以從與之相關(guān)的另一個模型中對數(shù)據(jù)透視表進(jìn)行類型轉(zhuǎn)換?
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 關(guān)系的模型中添加此屬性:
In your model that defines the BelongsToMany relationship add this property:
protected $casts = ['relationName.pivot.id' => 'string'];
更新
我想我們可以在這里使用 php7.0
中的匿名類,而不是為樞軸創(chuàng)建模型類:
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'];
}));
}
通常我更愿意為數(shù)據(jù)透視表創(chuàng)建模型或簡單地使用數(shù)據(jù)透視類
Generally i would prefer to create model for the pivot table or simply use a pivot class
這篇關(guān)于Laravel 在數(shù)據(jù)透視表中雄辯的 UUID的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!