CategoryRepository.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019-06-15
  6. * Time: 11:16
  7. */
  8. namespace App\Repositories;
  9. use App\Models\Category;
  10. use App\Models\CategoryTopic;
  11. class CategoryRepository {
  12. public function __construct(Category $category,CategoryTopic $categoryTopic) {
  13. $this->category = $category;
  14. $this->categoryTopic = $categoryTopic;
  15. }
  16. //列表
  17. public function lists($request)
  18. {
  19. $where = [];
  20. if(isset($request['is_suggest']) && $request['is_suggest'] == 1){
  21. $where [] = ['is_suggest', 1];
  22. $data = [];
  23. }else{
  24. $where [] = ['is_open', 1];
  25. $data = [
  26. [
  27. 'id' => -2,
  28. 'name' => '热门',
  29. 'img' => config('customer.category_hot_img')
  30. ],
  31. [
  32. 'id' => -1,
  33. 'name' => '我的话题',
  34. 'img' => config('customer.category_my_img')
  35. ]
  36. ];
  37. }
  38. $categories = $this->category->select('id', 'name', 'img')->where($where)->orderBy('sort', 'asc')->limit(50)->get()->toArray();
  39. return array_merge($data, $categories);
  40. }
  41. //获取多个话题
  42. public function getTopics($ids){
  43. $ids = explode(',',$ids);
  44. return $this->categoryTopic
  45. ->leftJoin('topic', 'category_topic.topic_id', '=', 'topic.id')
  46. ->where('topic.is_suggest',1)
  47. ->whereIn('category_topic.category_id',$ids)
  48. ->select('topic.id','topic.name','topic.is_suggest')
  49. ->groupBy('topic.id')
  50. ->get();
  51. }
  52. }