Laravel eloquent UUID in a pivot table

后端 未结 2 2063
Happy的楠姐
Happy的楠姐 2021-01-07 06:27

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

2条回答
  •  再見小時候
    2021-01-07 06:41

    I think this might be what you want:

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

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

    Update

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

    i didn't test this code so i don't know if it will work or not, this is just an idea

    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

提交回复
热议问题