Is this a good way to include the previous and next record?

Posted 3 months ago by MiguelStevens

I'm wondering if my method of including the next and previous records, are according to good (OOP/Laravel) standards.

In my Model class, I include the following methods

class PortfolioItem extends Model
{
  
    public function getPreviousRecord()
    {
        return PortfolioItem::where('id', '<', $this->id)->first();
    }

    public function getNextRecord()
    {
        return PortfolioItem::where('id', '>', $this->id)->first();
    }
}

Then I check in my view if the returned result is not null, and render those items.

@if($portfolioItem->getPreviousRecord())
    <div>
        <a class="d-block" href="{{ route('portfolio.show', $portfolioItem->getPreviousRecord()->id) }}">
            Vorig Werk
        </a>
        <h5 class="d-none d-md-block">{{ $portfolioItem->getPreviousRecord()->title }}</h5>
    </div>
@endif
    

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.