CategoryRepository.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. $data = [
  25. [
  26. 'id' => -2,
  27. 'name' => '热门',
  28. 'img' => config('customer.category_hot_img')
  29. ],
  30. [
  31. 'id' => -1,
  32. 'name' => '我的话题',
  33. 'img' => config('customer.category_my_img')
  34. ]
  35. ];
  36. }
  37. $categories = $this->category->select('id', 'name', 'img')->where($where)->limit(50)->get()->toArray();
  38. return array_merge($data, $categories);
  39. }
  40. //获取多个话题
  41. public function getTopics($ids){
  42. $ids = explode(',',$ids);
  43. return $this->categoryTopic
  44. ->leftJoin('topic', 'category_topic.topic_id', '=', 'topic.id')
  45. ->where('topic.is_suggest',1)
  46. ->whereIn('category_topic.category_id',$ids)
  47. ->select('topic.id','topic.name','topic.is_suggest')
  48. ->groupBy('topic.id')
  49. ->get();
  50. }
  51. }