0

I made a custom and simple blog module.

I'm trying to display, on the single article page, the next article and the previous one.

Then I tried, with help I got here, in the model

    public function onRun (){

     $this->articles = $this->getNextArticleAttribute();

     $this->articles = $this->getPreviousArticleAttribute();


   }

    public function getNextArticleAttribute() {

    return self::where('id','<', $this->id)->orderBy('id', 'ASC')->first();

}

    public function getPreviousArticleAttribute() {

    return self::where('id','>', $this->id)->orderBy('id', 'ASC')->first();

And in the single page

            {% set previous = __SELF__.previousArticle() %}

            <div class="wrapper-small">
              <div id="single-pagination" class="blog-pagination fixed">
                <ul class="pagination clearfix">
                  <li class="prev"><a href="/news/{{previous.slug}}" class="pseudo-ajax">
                    <span class="text">Précédent</span>
                    <span class="page-image"><span><img src="{{previous.newsimage.path}}" alt="SEO Name" /></span></span>
                  </a>

              {% set next = __SELF__.nextArticle() %}

                  </li>
                  <li class="next "><a href="/news/{{next.slug}}" class="pseudo-ajax">
                    <span class="text">Suivant</span>
                    <span class="page-image"><span><img src="{{next.newsimage.path}}" alt="SEO Name" /></span></span>
                  </a></li>
                </ul>
              </div> <!-- END .single-pagination -->
            </div> <!-- END .wrapper-small -->

So I really don't know what I'm missing...

2
  • can you please share what is not working and what output you get for the current script so we can help you Jun 25, 2018 at 14:06
  • Hello @HardikSatasiya, thanks for your answer. Actually this code doesn't output anything (no slug in {{previous.slug}} and no image in {{next.newsimage.path}}). I don't know how to make a dd to see if I get anything else...
    – DVst
    Jun 25, 2018 at 14:59

1 Answer 1

1

First you have to declare previousArticle and nextArticle as accessors in the model:

public function getNextArticleAttribute() { and public function getPreviousArticleAttribute() {

And then, from your model instance, you can get the next or previous article so:

$model = Article::find($id); // instantiate an article model
$model->nextArticle; // return a model article
$model->previousArticle; // both without parentesis

On the other hand, I think that you don't need to call $post = Article::where('slug',$slug)->first(); in both functions because $this is actually the current article, so, nextArticle may look like this:

public function getNextArticleAttribute() {
    return self::where('id','>', $this->id)->orderBy('id', 'ASC')->first();
}
2
  • thanks for your help. Unfortunately I'm still stuck and sure I'm missing something.
    – DVst
    Jul 3, 2018 at 17:32
  • @DVst I see your modifications and I think I confused you by giving you two alternatives. I will modify my answer to give you the best alternative from my point of view. Jul 4, 2018 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.