polymorphism - Laravel 5 polymorphic relationship with pivot table -
i have following tables: different content types: content1, content2 pivot table: content_module different module types: module1, module2
i trying build relationships between content* tables , module* tables each content can have module mixed in order.
for example: content1 can have module1, module1, module2, module1, module2, module2
can recommend best approach on solving issue. should use polymorphic relationship or raw query?
if can use polymorphic relationship, there example can at?
i ended writing own function getmodules() in repository of content model.
public function getmodules() { $contentabletype = get_class($this->model); // includes model's namespace mynamespace\content $contentmodules = db::table('content_modules') ->select(['moduleable_id', 'moduleable_type']) ->where('status', 1) ->where('contentable_id', $this->model->id) ->where('contentable_type', $contentabletype) ->orderby('position', 'asc') ->get(); $modules = []; foreach ($contentmodules $cm) { $model = app::make('mynamespace\\' . $cm->moduleable_type); $record = $model->find($cm->moduleable_id); $modules[] = [ 'type' => strtolower($cm->moduleable_type), 'data' => $record, ]; } return $modules; } this had done in 1 query select records content_modules , each record, separate query retrieve modules corresponding tables. assumes each module has own table , each table has different columns.
Comments
Post a Comment