Skip to content
This repository was archived by the owner on Mar 13, 2023. It is now read-only.

Commit fccb4d7

Browse files
committed
Add next and previous post ton single blog, package and tutorial view
1 parent 9db5f72 commit fccb4d7

File tree

9 files changed

+174
-57
lines changed

9 files changed

+174
-57
lines changed

app/Http/Controllers/BlogController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public function post(string $slug)
7171
$post = $this->postRepository->findBySlug($slug);
7272
$post->addView();
7373

74-
return view('frontend.blog.post', compact('post'));
74+
$prevPost = $this->postRepository->prevPost($post->id);
75+
$nextPost = $this->postRepository->nextPost($post->id);
76+
77+
return view('frontend.blog.post', compact('post', 'prevPost', 'nextPost'));
7578
}
7679
}

app/Http/Controllers/PackageController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public function post(string $slug)
7070
$package = $this->repository->findBySlug($slug);
7171
$package->addView();
7272

73-
return view('frontend.packages.post', compact('package'));
73+
$prevPost = $this->repository->prevPost($package->id);
74+
$nextPost = $this->repository->nextPost($package->id);
75+
76+
return view('frontend.packages.post', compact('package', 'prevPost', 'nextPost'));
7477
}
7578
}

app/Http/Controllers/TutorialController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public function post(string $slug)
6969
$tutorial = $this->repository->findBySlug($slug);
7070
$tutorial->addView();
7171

72-
return view('frontend.tutorials.post', compact('tutorial'));
72+
$prevPost = $this->repository->prevPost($tutorial->id);
73+
$nextPost = $this->repository->nextPost($tutorial->id);
74+
75+
return view('frontend.tutorials.post', compact('tutorial', 'prevPost', 'nextPost'));
7376
}
7477
}

app/Repositories/PackageRepository.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,36 @@ public function findBySlug(string $slug)
104104
->where('slug', $slug)
105105
->firstOrFail();
106106
}
107+
108+
/**
109+
* Return the previous package to the id set in param
110+
*
111+
* @param int $id
112+
* @return \Illuminate\Database\Eloquent\Model|null|object|static
113+
*/
114+
public function prevPost(int $id)
115+
{
116+
return $this->model->newQuery()
117+
->select('title', 'resume', 'slug')
118+
->where('id', '<', $id)
119+
->where('is_approved', true)
120+
->orderBy('id', 'DESC')
121+
->first();
122+
}
123+
124+
/**
125+
* Return the next package to the id set in param
126+
*
127+
* @param int $id
128+
* @return \Illuminate\Database\Eloquent\Model|null|object|static
129+
*/
130+
public function nextPost(int $id)
131+
{
132+
return $this->model->newQuery()
133+
->select('title', 'resume', 'slug')
134+
->where('id', '>', $id)
135+
->where('is_approved', true)
136+
->orderBy('id', 'ASC')
137+
->first();
138+
}
107139
}

app/Repositories/PostRepository.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,36 @@ public function findBySlug(string $slug)
114114
->where('slug', $slug)
115115
->firstOrFail();
116116
}
117+
118+
/**
119+
* Return the previous post to the id set in param
120+
*
121+
* @param int $id
122+
* @return \Illuminate\Database\Eloquent\Model|null|object|static
123+
*/
124+
public function prevPost(int $id)
125+
{
126+
return $this->model->newQuery()
127+
->select('title', 'excerpt', 'slug')
128+
->where('id', '<', $id)
129+
->where('status', '=', Post::PUBLISHED)
130+
->orderBy('id', 'DESC')
131+
->first();
132+
}
133+
134+
/**
135+
* Return the next post to the id set in param
136+
*
137+
* @param int $id
138+
* @return \Illuminate\Database\Eloquent\Model|null|object|static
139+
*/
140+
public function nextPost(int $id)
141+
{
142+
return $this->model->newQuery()
143+
->select('title', 'excerpt', 'slug')
144+
->where('id', '>', $id)
145+
->where('status', '=', Post::PUBLISHED)
146+
->orderBy('id', 'ASC')
147+
->first();
148+
}
117149
}

app/Repositories/TutorialRepository.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,36 @@ public function findBySlug(string $slug)
104104
->where('slug', $slug)
105105
->firstOrFail();
106106
}
107+
108+
/**
109+
* Return the previous package to the id set in param
110+
*
111+
* @param int $id
112+
* @return \Illuminate\Database\Eloquent\Model|null|object|static
113+
*/
114+
public function prevPost(int $id)
115+
{
116+
return $this->model->newQuery()
117+
->select('title', 'resume', 'slug')
118+
->where('id', '<', $id)
119+
->where('is_published', true)
120+
->orderBy('id', 'DESC')
121+
->first();
122+
}
123+
124+
/**
125+
* Return the next package to the id set in param
126+
*
127+
* @param int $id
128+
* @return \Illuminate\Database\Eloquent\Model|null|object|static
129+
*/
130+
public function nextPost(int $id)
131+
{
132+
return $this->model->newQuery()
133+
->select('title', 'resume', 'slug')
134+
->where('id', '>', $id)
135+
->where('is_published', true)
136+
->orderBy('id', 'ASC')
137+
->first();
138+
}
107139
}

resources/views/frontend/blog/post.blade.php

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,28 @@
6464
</div>
6565
<div class="post_pagination">
6666
<div class="container">
67-
<a href="javascript:;" class="pagination__left">
68-
<svg id="icon-arrow-thin-left" fill="none" viewBox="0 0 31.344 105.69" stroke="currentColor" stroke-width="3px" fill-rule="evenodd" width="100%" height="100%">
69-
<path d="M29.844 2.86l-25 50 25 50"></path>
70-
</svg>
71-
<div class="pagination__content">
72-
<h4>Puphpeteer: A Puppeteer bridge for PHP</h4>
73-
<p>Puphpeteer by Johann Pardanaud is PHP bridge for Google Chrome&#8217;s Puppeteer headless chrome Node.js API. Learn h&hellip;</p>
74-
</div>
75-
</a>
76-
<a href="javascript:;" class="pagination__right">
77-
<svg id="icon-arrow-thin-right" fill="none" viewBox="0 0 30.69 103" stroke="currentColor" stroke-width="3px" fill-rule="evenodd" width="100%" height="100%">
78-
<path d="M4.19 1.51l25 50-25 50"></path>
79-
</svg>
80-
<div class="pagination__content">
81-
<h4>Built with Jigsaw – the Ultimate Showcase of Web Sites Built with Jigsaw</h4>
82-
<p>“Built with Jigsaw” is a gallery of websites made with Jigsaw, and includes helpful articles about using…</p>
83-
</div>
84-
</a>
67+
@if(!is_null($prevPost))
68+
<a href="{{ route('blog.post', ['slug' => $prevPost->slug]) }}" class="pagination__left">
69+
<svg id="icon-arrow-thin-left" fill="none" viewBox="0 0 31.344 105.69" stroke="currentColor" stroke-width="3px" fill-rule="evenodd" width="100%" height="100%">
70+
<path d="M29.844 2.86l-25 50 25 50"></path>
71+
</svg>
72+
<div class="pagination__content">
73+
<h4>{{ $prevPost->title }}</h4>
74+
<p>{{ str_limit($prevPost->excerpt, 110) }}</p>
75+
</div>
76+
</a>
77+
@endif
78+
@if(!is_null($nextPost))
79+
<a href="{{ route('blog.post', ['slug' => $nextPost->slug]) }}" class="pagination__right">
80+
<svg id="icon-arrow-thin-right" fill="none" viewBox="0 0 30.69 103" stroke="currentColor" stroke-width="3px" fill-rule="evenodd" width="100%" height="100%">
81+
<path d="M4.19 1.51l25 50-25 50"></path>
82+
</svg>
83+
<div class="pagination__content">
84+
<h4>{{ $nextPost->title }}</h4>
85+
<p>{{ str_limit($nextPost->excerpt, 110) }}</p>
86+
</div>
87+
</a>
88+
@endif
8589
</div>
8690
</div>
8791

resources/views/frontend/packages/post.blade.php

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,28 @@
6464
</div>
6565
<div class="post_pagination">
6666
<div class="container">
67-
<a href="javascript:;" class="pagination__left">
68-
<svg id="icon-arrow-thin-left" fill="none" viewBox="0 0 31.344 105.69" stroke="currentColor" stroke-width="3px" fill-rule="evenodd" width="100%" height="100%">
69-
<path d="M29.844 2.86l-25 50 25 50"></path>
70-
</svg>
71-
<div class="pagination__content">
72-
<h4>Puphpeteer: A Puppeteer bridge for PHP</h4>
73-
<p>Puphpeteer by Johann Pardanaud is PHP bridge for Google Chrome&#8217;s Puppeteer headless chrome Node.js API. Learn h&hellip;</p>
74-
</div>
75-
</a>
76-
<a href="javascript:;" class="pagination__right">
77-
<svg id="icon-arrow-thin-right" fill="none" viewBox="0 0 30.69 103" stroke="currentColor" stroke-width="3px" fill-rule="evenodd" width="100%" height="100%">
78-
<path d="M4.19 1.51l25 50-25 50"></path>
79-
</svg>
80-
<div class="pagination__content">
81-
<h4>Built with Jigsaw – the Ultimate Showcase of Web Sites Built with Jigsaw</h4>
82-
<p>“Built with Jigsaw” is a gallery of websites made with Jigsaw, and includes helpful articles about using…</p>
83-
</div>
84-
</a>
67+
@if(!is_null($prevPost))
68+
<a href="{{ route('blog.post', ['slug' => $prevPost->slug]) }}" class="pagination__left">
69+
<svg id="icon-arrow-thin-left" fill="none" viewBox="0 0 31.344 105.69" stroke="currentColor" stroke-width="3px" fill-rule="evenodd" width="100%" height="100%">
70+
<path d="M29.844 2.86l-25 50 25 50"></path>
71+
</svg>
72+
<div class="pagination__content">
73+
<h4>{{ $prevPost->title }}</h4>
74+
<p>{{ str_limit($prevPost->resume, 110) }}</p>
75+
</div>
76+
</a>
77+
@endif
78+
@if(!is_null($nextPost))
79+
<a href="{{ route('blog.post', ['slug' => $nextPost->slug]) }}" class="pagination__right">
80+
<svg id="icon-arrow-thin-right" fill="none" viewBox="0 0 30.69 103" stroke="currentColor" stroke-width="3px" fill-rule="evenodd" width="100%" height="100%">
81+
<path d="M4.19 1.51l25 50-25 50"></path>
82+
</svg>
83+
<div class="pagination__content">
84+
<h4>{{ $nextPost->title }}</h4>
85+
<p>{{ str_limit($nextPost->resume, 110) }}</p>
86+
</div>
87+
</a>
88+
@endif
8589
</div>
8690
</div>
8791

resources/views/frontend/tutorials/post.blade.php

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,28 @@
6464
</div>
6565
<div class="post_pagination">
6666
<div class="container">
67-
<a href="javascript:;" class="pagination__left">
68-
<svg id="icon-arrow-thin-left" fill="none" viewBox="0 0 31.344 105.69" stroke="currentColor" stroke-width="3px" fill-rule="evenodd" width="100%" height="100%">
69-
<path d="M29.844 2.86l-25 50 25 50"></path>
70-
</svg>
71-
<div class="pagination__content">
72-
<h4>Puphpeteer: A Puppeteer bridge for PHP</h4>
73-
<p>Puphpeteer by Johann Pardanaud is PHP bridge for Google Chrome&#8217;s Puppeteer headless chrome Node.js API. Learn h&hellip;</p>
74-
</div>
75-
</a>
76-
<a href="javascript:;" class="pagination__right">
77-
<svg id="icon-arrow-thin-right" fill="none" viewBox="0 0 30.69 103" stroke="currentColor" stroke-width="3px" fill-rule="evenodd" width="100%" height="100%">
78-
<path d="M4.19 1.51l25 50-25 50"></path>
79-
</svg>
80-
<div class="pagination__content">
81-
<h4>Built with Jigsaw – the Ultimate Showcase of Web Sites Built with Jigsaw</h4>
82-
<p>“Built with Jigsaw” is a gallery of websites made with Jigsaw, and includes helpful articles about using…</p>
83-
</div>
84-
</a>
67+
@if(!is_null($prevPost))
68+
<a href="{{ route('blog.post', ['slug' => $prevPost->slug]) }}" class="pagination__left">
69+
<svg id="icon-arrow-thin-left" fill="none" viewBox="0 0 31.344 105.69" stroke="currentColor" stroke-width="3px" fill-rule="evenodd" width="100%" height="100%">
70+
<path d="M29.844 2.86l-25 50 25 50"></path>
71+
</svg>
72+
<div class="pagination__content">
73+
<h4>{{ $prevPost->title }}</h4>
74+
<p>{{ str_limit($prevPost->resume, 110) }}</p>
75+
</div>
76+
</a>
77+
@endif
78+
@if(!is_null($nextPost))
79+
<a href="{{ route('blog.post', ['slug' => $nextPost->slug]) }}" class="pagination__right">
80+
<svg id="icon-arrow-thin-right" fill="none" viewBox="0 0 30.69 103" stroke="currentColor" stroke-width="3px" fill-rule="evenodd" width="100%" height="100%">
81+
<path d="M4.19 1.51l25 50-25 50"></path>
82+
</svg>
83+
<div class="pagination__content">
84+
<h4>{{ $nextPost->title }}</h4>
85+
<p>{{ str_limit($nextPost->resume, 110) }}</p>
86+
</div>
87+
</a>
88+
@endif
8589
</div>
8690
</div>
8791

0 commit comments

Comments
 (0)