CategoryRepository.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  20. $where = [];
  21. if(!empty($request['is_suggest'])){
  22. $where[] = ['is_suggest', '=', $request['is_suggest']];
  23. }
  24. return $this->category->where($where)->paginate($perPage);
  25. }
  26. //获取多个话题
  27. public function getTopics($ids){
  28. $ids = explode(',',$ids);
  29. return $this->categoryTopic
  30. ->leftJoin('topic', 'category_topic.topic_id', '=', 'topic.id')
  31. ->where('topic.is_suggest',1)
  32. ->whereIn('category_topic.category_id',$ids)
  33. ->select('topic.id','topic.name','topic.is_suggest')
  34. ->groupBy('topic.id')
  35. ->get();
  36. }
  37. }