skip to Main Content

How can I get the current model in the laravel nova actions field function? Can’t find it out …

public function fields(NovaRequest $request)
{
    Log::info($request->model());

    return [
        //
    ];
}

2

Answers


  1. Chosen as BEST ANSWER

    The only way I figured out was the solution to paste the model to the constructor of the action class:

    public function actions(NovaRequest $request)
    {
        return [
            (new ActionsMyAction($this))->onlyOnTableRow()
        ];
    }
    

    If this is really the best solution? Is there really no way to grab this from the NovaRequest?


  2. Yes, there is a way to access the resources from the NovaRequest.
    I don’t believe this is officially documented by Laravel Nova, but here are the two implementations for Index and Detail views.

    public function fields(NovaRequest $request)
    {
        // Resources selected on index view.
        // Returns array of resource ids: [1, 2, 3];
        $resourceIds = $request->query('resources');
    
        // Resource on detail view
        // Returns string of resource id: "1"
        $resourceId = $request->query('resourceId');
    
        return [
           // Your Fields
        ];
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search