Laravel nova action - get current model instance on fields()
Asked Answered
A

6

6

Laravel 5.8 Nova 2.0

In nova action

public function fields()
{
    return [];
}

Is there any way to access currently selected rows here?

Alfieri answered 4/7, 2019 at 13:19 Comment(0)
K
5

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
   ];
}
Kumar answered 26/7, 2021 at 9:33 Comment(1)
seems to make sense, will be helpful, if you will add some reference to this answer.Alfieri
T
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.

Tades answered 24/4, 2023 at 4:48 Comment(0)
A
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).

Anchie answered 2/10, 2019 at 5:21 Comment(6)
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?Alfieri
Nova actions are not tied to a particular model instance (or a group of models), but to the resource definition. Does that make sense?Anchie
Is there any way to get respective model from a particular resource?Alfieri
Only within the "handle" methodAnchie
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.Peng
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.Banff
J
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);
Juridical answered 7/11, 2019 at 16:35 Comment(0)
K
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();
Kershaw answered 26/7, 2021 at 15:2 Comment(1)
If it will work, you can just do Model::find(request()->resourceId) . do not have Nova now, so can not confirm though.Alfieri
T
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.

Thersathersites answered 20/10, 2022 at 11:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.