Table, access previous or another record data

Is it possible to access the previous record in a table? or the first one? From a record you can access the data of another.
6 Replies
Mohamed Ayaou
Mohamed Ayaou4d ago
Can you clarify more what do you mean with a previous records and where you are trying to access it?
H.Bilbao
H.BilbaoOP4d ago
I need the value of the column of the previous row and first row. And If I filter table or sort update with new values.
Mohamed Ayaou
Mohamed Ayaou4d ago
Sir, you still didn't clarify what do you mean with the previous row how many rows in the table page and in the previous page as well.
H.Bilbao
H.BilbaoOP3d ago
table has only one page and what I want is data from a column in the previous row. For example, in row id 34, I want the data from column 1 of row id 33 and from column 2 of row id 1.
Mohamed Ayaou
Mohamed Ayaou3d ago
Oh, I do not think Filament is supporting something like that direclty
Aleved
Aleved2d ago
You'd want to do this within your model, something like this:
public function getPreviousRow()
{
return static::where('id', '<', $this->id)
->orderBy('id', 'desc')
->first();
}

public function getNextRow()
{
return static::where('id', '>', $this->id)
->orderBy('id', 'asc')
->first();
}
public function getPreviousRow()
{
return static::where('id', '<', $this->id)
->orderBy('id', 'desc')
->first();
}

public function getNextRow()
{
return static::where('id', '>', $this->id)
->orderBy('id', 'asc')
->first();
}
If you're using mysql 8 (or postgres) you can also look at the window functions, and probably just join them directly. The functions are LEAD() and LAG().

Did you find this page helpful?