CategoryRepository.php 1.3 KB

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