/** * @return static */ public function getList() { if ($this->model->hasParent()) { $model = ArticleModel::whereCategoryId($this->model->getAttribute('id')); } else { $relations = $this->getRelationCategoryList(); $list = Collection::make(); $list->push($this->model->getAttribute('id')); foreach ($relations as $relation) { $list->push($relation->getId()); } $model = ArticleModel::whereIn('category_id', $list->toArray()); } $data = $model->paginate(15); //$list = Collection::make(); //foreach($data as $value) { // $list->push(new Article($value->getAttribute('id'))); //} return $data; }
/** * @param $template * @param array $opinions * @return mixed */ protected function callArticle($template, $opinions = []) { $articles = Collection::make(); if (isset($opinions['category'])) { if (is_array($opinions['category'])) { $articles = Article::whereIn('category_id', $opinions['category']); } else { $articles = Article::whereCategoryId($opinions['category']); } if (isset($opinions['offset']) && $opinions['offset'] > 0) { $articles->skip($opinions['offset']); } if (isset($opinions['limit']) && $opinions['limit'] > 0) { $articles->take($opinions['limit']); } $articles = $articles->get(); if (isset($opinions['thumbnail']) && is_array($opinions['thumbnail'])) { foreach ($articles as $key => $article) { preg_match_all("/<img([^>]*)\\s*src=('|\")([^'\"]+)('|\")/", $article->content, $matches); $hash = ''; $thumbnail = ''; if ($matches && $matches[3]) { $matches = array_unique($matches[3]); if ($matches[0] && $this->file->exists(public_path($matches[0]))) { $thumbnail = $matches[0]; $hash = hash_file('md5', public_path($matches[0]), false); } } if ($thumbnail && $hash) { $path = '/uploads/thumbnails/' . $opinions['thumbnail']['width'] . 'X' . $opinions['thumbnail']['height'] . '/' . $hash . '.' . $this->file->extension(public_path($thumbnail)); $directory = public_path('/uploads/thumbnails/' . $opinions['thumbnail']['width'] . 'X' . $opinions['thumbnail']['height'] . '/'); if (!$this->file->isDirectory($directory)) { $this->file->makeDirectory($directory, 0777, true, true); } if (!$this->file->exists(public_path($path))) { $image = Image::make($thumbnail, $opinions['thumbnail']); $image->save(public_path($path)); } $article->thumbnail = $path; } $articles->put($key, $article); } } } return $this->view->make($template)->withArticles($articles); }