【发布时间】:2020-04-25 01:02:41
【问题描述】:
我有一个 Image 表如下:
Schema::create('images', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('path');
$table->unsignedBigInteger('imageable_id')->nullable();
$table->string('imageable_type')->nullable();
$table->unsignedBigInteger('image_properties_id')->nullable();
$table->unsignedBigInteger('parent_id')->nullable();
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
});
我想与图像表建立多态关系,它适用于大多数部分,但是这里的问题是某些模型(例如 Post)可以具有用于不同目的的图像,例如,帖子可以具有用于帖子封面的图像以及帖子内容中使用的图像。 是否可以仅访问属于帖子封面而不是内容的图像?
我知道我可以使用 morphmap 定义自定义类型,但是是否可以定义两个指向同一个 Model 的不同类型并使用这些类型来过滤结果?
【问题讨论】:
-
您打算如何为给定帖子的任何图像存储
type信息? -
"imageable_type" 是连接模型名称和图像用例的结果。 “imageable_type”的几个例子是:“post/content”或“post/cover”