/** * 添加一个可被引入的项. * * @param Includable $includable * * @throws Exception */ public function add(Includable $includable) { if ($includable->isNested()) { $parent_includable = $this->getIncludable($includable->getParentName()); if (null === $parent_includable) { throw new Exception('You must define includable ' . $includable->getParentName()); } $parent_includable->addChildren($includable); } $this->available_includes[$includable->getName()] = $includable; $this->foreign_keys[$includable->getName()] = $includable->getForeignKey(); }
/** * 使用 user_id 字段引入关联的用户. * * @param $default_columns */ public function includeUser($default_columns) { $available_include = Includable::make('user')->setDefaultColumns($default_columns)->setAllowColumns(User::$includable)->setForeignKey('user_id'); app(IncludeManager::class)->add($available_include); }
/** * 引入帖子每个的评论发布者. * * @param $default_columns */ public function includeRepliesUser($default_columns) { $available_include = Includable::make('replies.user')->setDefaultColumns($default_columns)->setAllowColumns(User::$includable)->nested('replies'); app(IncludeManager::class)->add($available_include); }
/** * 使用 user_id 字段引入关联的用户. * * @param $default_columns */ public function includeTopic($default_columns) { $available_include = Includable::make('topic')->setDefaultColumns($default_columns)->setAllowColumns(User::$includable)->withTrashed()->setForeignKey('topic_id'); app(IncludeManager::class)->add($available_include); }