我正在处理多态关系并创建了一个多态表,其中的列如下
posts -:
| id(integer) | title(string) | body(text) |
videos -:
| id(integer) | title(string) | url(text) |
comments -:
| id(integer) | title(string) | commentable_id(integer) | commentable_type(string) |
默认的commentable_type将分别是App\Models\Post或App\Models\Video。
我不想将这些值用于commentable_type想要一些自定义值,找到了一种使用 morphMap 的方法,例如 -
use Illuminate\Database\Eloquent\Relations\Relation;
Relation::morphMap([
'posts' => 'App\Models\Post',
'videos' => 'App\Models\Video',
]);
寻找其他解决方案,例如可以在模型中使用的标志或方法以实现某些结果