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

eugenefvdm's avatar

PHP Append Fluent Method to custom repo's Static Method Call in Laravel Nova

I'm using a static method in Laravel Nova to abstract a slightly complex currency field.

In the Nova fields method, I use this static call:

MyNovaRepo::Money('Price');

Refers to:

use Vyuldashev\NovaMoneyField\Money;

class MyNovaRepo
{

    public static function Money($field) {
        return Money::make($field, config('nova.currency'))->locale(config('app.locale'))->sortable()->storedInMinorUnits();
    }

}

This works pretty well but now I want to append some of Laravel Nova's fluent methods, e.g. hideFromIndex()

To be honest I've watched videos on changing a normal object class method into a static method time and time again but I simply don't get it.

For starters I guess I need something like this:


function hideFromIndex() {
    // Do something to ensure the calling context is available
    // Append ->hideFromIndex to the calling context
    return $this;
}

I just don't get it. Am I supposed to declare a constructor that "converts" my static object into a dynamic object? How?

Then once I've crossed that bridge, how would I get the Nova context available? Add $this in the constructor?

0 likes
1 reply
eugenefvdm's avatar

After thinking about this for weeks, I just tried:

 public function fields(Request $request)
    {
        return [

            ID::make()->sortable(),

		...

            NovaRepo::Money('Price')->hideFromIndex(),

Without doing anything in the NovaRepo static method, and it just works! In other words, I didn't have to create an empty return $this or reproduce the name of the method in my static method.

I guess I was put off by PhpStorm saying 'Method 'hideFromIndex' not found in $this` and the fact that autocomplete wasn't working. I'm still not 100% why it's working but it appears the problem was incredibly easy to solve.

Please or to participate in this conversation.