12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019-06-15
- * Time: 11:16
- */
- namespace App\Repositories;
- use App\Models\Category;
- use App\Models\CategorySuggest;
- use App\Models\CategoryTopic;
- class CategoryRepository {
- public function __construct(Category $category,CategoryTopic $categoryTopic, CategorySuggest $categorySuggest) {
- $this->category = $category;
- $this->categoryTopic = $categoryTopic;
- $this->categorySuggest = $categorySuggest;
- }
- //列表
- public function lists($request)
- {
- $where = [];
- if(isset($request['is_suggest']) && $request['is_suggest'] == 1){
- $where [] = ['is_suggest', 1];
- $data = [];
- }else{
- $where [] = ['is_open', 1];
- $data = [
- [
- 'id' => -2,
- 'name' => '热门',
- 'img' => config('customer.category_hot_img')
- ],
- [
- 'id' => -1,
- 'name' => '我的话题',
- 'img' => config('customer.category_my_img')
- ]
- ];
- }
- $categories = $this->category->select('id', 'name', 'img')->where($where)->orderBy('sort', 'asc')->limit(50)->get()->toArray();
- return array_merge($data, $categories);
- }
- //获取多个话题
- public function getTopics($ids){
- $ids = explode(',',$ids);
- return $this->categoryTopic
- ->leftJoin('topic', 'category_topic.topic_id', '=', 'topic.id')
- ->where('topic.is_suggest',1)
- ->whereIn('category_topic.category_id',$ids)
- ->select('topic.id','topic.name','topic.is_suggest')
- ->groupBy('topic.id')
- ->get();
- }
- //分类列表(推荐内容首页用)
- public function suggest()
- {
- $list = [
- [
- 'id' => 0,
- 'name' => '推荐',
- 'img' => config('customer.category_hot_img')
- ]
- ];
- $name = ['推荐'];
- $res = $this->categorySuggest->get();
- foreach($res as $item){
- $list[] = [
- 'id' => $item->category_id,
- 'name' => $item->category->name,
- 'img' => $item->category->img,
- ];
- $name[] = $item->category->name;
- }
- return [
- 'category_list' => $list,
- 'category_list_name' => $name,
- ];
- }
- }
|