Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Hala's avatar
Level 3

Query in nova relation

i have post resource and comment

when i show up post detail page i want to list comments order_by field not by id , how could i add that

when i added the query into relation it works but i want to effect only on nova not all project

0 likes
3 replies
bugsysha's avatar

Create a nova relationship.

public function novaComments()
{
  return $this->comments()->orderBy('columnName');
}

Then in Nova use:

HasMany::make('Comments', 'novaComments')
Hala's avatar
Level 3

this will make model Missy if i have multi relation need to have some query in nova

bugsysha's avatar
bugsysha
Best Answer
Level 61

Yes, if you make it messy, it will be messy. There is no way around it. What you can do is use indexQuery method on Nova Comment resource where you will add that ordering, but then it will affect relationships as well as when you visit Comments in Nova. Then you can based on request change that query, but it all depends on what you need.

Please or to participate in this conversation.