我对 Laravel 和 Eloquent 还很陌生,所以这很容易成为微不足道的事情,但无论如何我到目前为止一直缺少一个好的解决方案。所以这是我的问题:
我有一个主画廊,我希望用户能够将他需要的任何图像或视频上传到该主画廊,并从该画廊中选择图像/视频以在网站的其他地方显示(基本上我正在做的是集中多媒体内容都在一个地方)。
使用演绎法(我对 Eloquent 的了解很差)我发现这对我来说是最好的解决方案:
//Gallery Model
class Gallery extends Eloquent {
protected $table = 'gallery';
protected $guarded = array();
public static $rules = array();
public function images() {
return $this->morphMany('Image', 'imageable');
}
public function videos() {
return $this->morphMany('Video', 'videoable');
}
}
//About Orchestra Model
class OrchestraAbout extends Eloquent {
protected $table = 'orchestra_about';
protected $guarded = array();
public static $rules = array();
public function images() {
return $this->morphMany('Image', 'imageable');
}
public function videos() {
return $this->morphMany('Video', 'videoable');
}
}
//Image Model
class Image extends Eloquent {
protected $guarded = array();
public static $rules = array();
public function imageable()
{
return $this->morphTo();
}
}
//Video Model
class Video extends Eloquent {
protected $guarded = array();
public static $rules = array();
public function videoable()
{
return $this->morphTo();
}
}
当插入数据库时,我只需复制该行并仅更改 imageable_id /videoable_id 和 imageable_type/videoable_type 以匹配 OrchestraAbout 模型。
这里的任何人都知道更好的解决方案吗?
感谢所有聪明的答案!干杯!