Laravel 5.8 Nova 2.0
In nova action
public function fields()
{
return [];
}
Is there any way to access currently selected rows here?
You can get current model instance from NovaRequest. And you may create NovaRequest from \Illuminate\Http\Request that is passed to the method:
use Laravel\Nova\Http\Requests\NovaRequest;
use Illuminate\Http\Request;
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
// Model instance or null
$model = NovaRequest::createFrom($request)
->findModelQuery()
->first();
return [
// Your fields here
];
}
No, for two reasons:
1) fields
is called when the resource loads, not when the action dialog is displayed
2) The concept of "currently selected" really only exists on the client (browser) side
You can only access the selected rows in the handle
PHP method (i.e., after submit, you have $models
).
Sometimes when I'm on the details view and want to perform an action on that record, but also want the current records data in the fields (maybe for help text), I grab it from the URL.
//Get the URL
$explodeUrl = explode('/', strrev($_SERVER['HTTP_REFERER']), 2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With