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 [
//
];
}
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?
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
];
}