6

Laravel 5.8 Nova 2.0

In nova action

public function fields()
{
    return [];
}

Is there any way to access currently selected rows here?

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105

6 Answers6

3

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

Don't know if anyone solved this but I got the same issue and found an article.

According to this article, you can create a public method setResource in action class and when registering action in nova resource, set the current resource of each row: $action->setResource($this->resource). Then, in fields method, you can add your logic with resource by using $this ->resource.

A note for this method is $this->resource can be null or an null model (a model class but no attribute). Thus, you must check the resource property if it is null before adding any logic.

simpsons3
  • 880
  • 1
  • 11
  • 29
1

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).

udog
  • 1,490
  • 2
  • 17
  • 30
  • When resource loads, the resource is of a model, correct, I wanted that in fields. If the model is not available at that moment, how we can display the resource? – Prafulla Kumar Sahu Oct 02 '19 at 05:26
  • Nova actions are not tied to a particular model instance (or a group of models), but to the resource definition. Does that make sense? – udog Oct 02 '19 at 05:44
  • Is there any way to get respective model from a particular resource? – Prafulla Kumar Sahu Oct 02 '19 at 05:52
  • 1
    Only within the "handle" method – udog Oct 02 '19 at 14:03
  • 1
    In my opinion this is a "design flaw" in Nova, because yes we understand the reasons why it's so, but Actions are one of the most powerful features and the lack of this ability stifles possibilities. – Eugene van der Merwe Mar 01 '21 at 19:52
  • I totally agree @EugenevanderMerwe it would be so powerful if we could base the fields on the amount of hasmany records selected for instance, and add model data in the actions view. we are looking in customizing to make this work. – Thomas Lang Apr 08 '21 at 08:25
1

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);
mcornille
  • 343
  • 2
  • 4
  • 17
1

I run the action only on the detail page then get the single model data like so:

$model = DB::table('something')->where('id', request()->resourceId)->first();
0

You can define a handleRequest function and retrieve the model that way. For instance

public function handleRequest(ActionRequest $request)
    {
        $this->model = $request->targetModel(); //I am setting a custom variable here.
        parent::handleRequest($request);
    }

You can then use the $this->model() in your handle() method.

Very useful for standalone actions.

prog_24
  • 771
  • 1
  • 12
  • 26