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

DavidPetrov's avatar

Adding an action for a certain hasMany relationship field

Hey, everybody!

Scenario is the following: I've got a Warehouse with a lot of Item which I'm displaying via a hasMany relationship on the Warehouse resource page. So far so great, now I'd like to be able to select given items from the warehouse and implement a custom action for them, called TransferItems, which will basically require one additional field (a Select for the target Warehouse), and everything further should be a breeze.

Now, the problem is that, suprisingly, according to the docs custom actions for relationship fields are only available for belongsToMany (for a reason unknown to me), so I thought I must be missing something.

The point being, as you can imagine, that Transfers only occur between two given warehouses, so that means that I don't actually need the action globally assigned on the Item resource, but only for the items of a given warehouse. So, would there be any way to achieve that in Laravel Noval (which is simply a great eliviator of casual developer suffering!)?

class Warehouse extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\Models\Warehouse::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'name';

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make(__('ID'), 'id')->sortable(),
			Text::make('Name')->sortable(),
			HasMany::make('Devices')//->actions() ??
        ];
    }
    //...
}
0 likes
0 replies

Please or to participate in this conversation.