PostRepository.php 830 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php namespace App\Repositories;
  2. use App\Models\Post;
  3. class PostRepository {
  4. protected $post;
  5. public function __construct(Post $post)
  6. {
  7. $this->post = $post;
  8. }
  9. private function queryWithUserAndTags()
  10. {
  11. return $this->post->with('user', 'tags')
  12. ->orderBy('posts.created_at', 'desc');
  13. }
  14. public function getWithUserAndTagsPaginate($n)
  15. {
  16. return $this->queryWithUserAndTags()->paginate($n);
  17. }
  18. public function getWithUserAndTagsForTagPaginate($tag, $n)
  19. {
  20. return $this->queryWithUserAndTags()
  21. ->whereHas('tags', function($q) use ($tag)
  22. {
  23. $q->where('tags.tag_url', $tag);
  24. })->paginate($n);
  25. }
  26. public function store($inputs)
  27. {
  28. return $this->post->create($inputs);
  29. }
  30. public function destroy($id)
  31. {
  32. $post = $this->post->findOrFail($id);
  33. $post->tags()->detach();
  34. $post->delete();
  35. }
  36. }