Previous and next record buttons in the view page

I would like to add previous and next buttons to my task view page but I am not been able to figure out how it will be done in yii2

I have done this in php but new to yii

Thanks

Create "getNext()" and "getPrev()" methods in your Task model.

Something like this:




    public function getNext()

    {

        return Task::find()

             ->where(['>', 'startDateTime', $this->startDateTime])

             ->orderBy(['startDateTime' => SORT_ASC])

             ->one();

    }


    public function getPrev()

    {

        return Task::find()

             ->where(['<', 'startDateTime', $this->startDateTime])

             ->orderBy(['startDateTime' => SORT_DESC])

             ->one();

    }



And in the view:




    <?= Html::a('Previous Task',  ['pmt/task/task-view', 'id' => $model->prev->id], ...); ?>

    <?= Html::a('Next Task',  ['pmt/task/task-view', 'id' => $model->next->id], ...); ?>



The code above is not good enough, but I hope you get the basic idea.

Thanks and this worked perfectly with minor changes in the URL

Perfectly? Are you sure?

Note that getNext() and getPrev() can return null, and they could skip tasks that are of the same startDateTime. :)

you got me there ;)