This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

krevitzr8584
krevitzr8584

I'm trying to develop a portfolio plugin and need to retrieve the previous or next item based on current item. In SQL, I would have to do the following:

select * from portfolio_items where id = (select min(id) from portfolio_items where id > 4)
select * from portfolio_items where id = (select max(id) from portfolio_items where id < 4)

where 4 is the id of the current item. How could I achieve this to make a method such that in the template, so that I can call {{ portfolioItem.previousUrl }} or {{ portfolioItem.nextUrl }}? Thanks in advance.

Hessel
Hessel

Did you found a solution for this?

jan-vince
jan-vince

You can look in Blog plugin source code. There are methods for previous/next records.

In Model you can create query like this:

public function nextRecord($currentId) {
$nextRecord = Record::where('id', '<>', $currentId)->where('id','>', $currentID)->orderBy('id', 'ASC')->first();
}

In Twig you can get nextRecord with {{ record.nextRecord }}.

You have to pass record to Twig from controller before you can use it.

Last updated

didier28788
didier28788

jan-vince said:

You can look in Blog plugin source code. There are methods for previous/next records.

In Model you can create query like this:

public function nextRecord($currentId) {
$nextRecord = Record::where('id', '<>', $currentId)->where('id','>', $currentID)->orderBy('id', 'ASC')->first();
}

In Twig you can get nextRecord with {{ record.nextRecord }}.

You have to pass record to Twig from controller before you can use it.

Hello jan-vince What do you mean by "You have to pass record to Twig from controller before you can use it"?

didier28788
didier28788

krevitzr8584 said:

I'm trying to develop a portfolio plugin and need to retrieve the previous or next item based on current item. In SQL, I would have to do the following:

select from portfolio_items where id = (select min(id) from portfolio_items where id > 4) select from portfolio_items where id = (select max(id) from portfolio_items where id < 4)

where 4 is the id of the current item. How could I achieve this to make a method such that in the template, so that I can call {{ portfolioItem.previousUrl }} or {{ portfolioItem.nextUrl }}? Thanks in advance.

Did you find a solution to this?

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.