0

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 2

0

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 Actions\MyAction($this))->onlyOnTableRow()
    ];
}

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

Sign up to request clarification or add additional context in comments.

Comments

0

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
    ];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.