MiguelStevens's avatar

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

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
    
0 likes
4 replies
bashy's avatar

@sharjeel That's only to show the buttons, not to get the previous and next data.

1 like
skimuli's avatar

Why don't you use Laravel simple paginate. You might face more challenges with that method. Portfolio::simplePaginate();

MiguelStevens's avatar

We're talking about showing next and previous models, not next and previous pages.. I hope that's clear from the question?

Please or to participate in this conversation.