12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php namespace App\Repositories;
- use App\Models\Post;
- class PostRepository {
- protected $post;
- public function __construct(Post $post)
- {
- $this->post = $post;
- }
- private function queryWithUserAndTags()
- {
- return $this->post->with('user', 'tags')
- ->orderBy('posts.created_at', 'desc');
- }
- public function getWithUserAndTagsPaginate($n)
- {
- return $this->queryWithUserAndTags()->paginate($n);
- }
- public function getWithUserAndTagsForTagPaginate($tag, $n)
- {
- return $this->queryWithUserAndTags()
- ->whereHas('tags', function($q) use ($tag)
- {
- $q->where('tags.tag_url', $tag);
- })->paginate($n);
- }
- public function store($inputs)
- {
- return $this->post->create($inputs);
- }
- public function destroy($id)
- {
- $post = $this->post->findOrFail($id);
- $post->tags()->detach();
- $post->delete();
- }
- }
|