tjsherrill's avatar

How to create a Nova action that links to the frontend

I have a bunch of Colleges in a database. I setup Nova to help manage them and that works great.

But I want the admin to be able to click a link to "View College" and have that link out to the "frontend" of the site so that they can see the changes they made.

So I setup an Action:

  public function handle(ActionFields $fields, Collection $models)
    {
        $model = $models->first();
        // Generate the link to the show action using the model's ID
        $link = route('colleges.show', ['college' => $model]);
        // You can return a simple success response with the link
        return Action::openInNewTab("View College", $link);
    }

If I dd the model, it shows the college, so that works. I registered the action and now the option is available in the three dots dropdown in nova (for that resource).

Biut when I click it, I get "missing parameter "college" in route" What gives?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To create a Nova action that links to the frontend and allows the admin to view the changes made to a College, you need to ensure that the route parameters are correctly passed. The error "missing parameter 'college' in route" suggests that the route is not receiving the expected parameter.

Here's a step-by-step solution to fix this issue:

  1. Ensure the Route is Correctly Defined: Make sure that your route definition in web.php (or the relevant routes file) expects the correct parameter. For example:

    Route::get('/colleges/{college}', [CollegeController::class, 'show'])->name('colleges.show');
    
  2. Update the Action Code: Modify the action to correctly pass the college ID to the route. The route helper function should be used with the correct parameter name.

    Here is the updated action code:

    use Laravel\Nova\Actions\Action;
    use Laravel\Nova\Fields\ActionFields;
    use Illuminate\Support\Collection;
    
    class ViewCollegeAction extends Action
    {
        public function handle(ActionFields $fields, Collection $models)
        {
            $model = $models->first();
            // Generate the link to the show action using the model's ID
            $link = route('colleges.show', ['college' => $model->id]);
            // You can return a simple success response with the link
            return Action::openInNewTab("View College", $link);
        }
    }
    
  3. Register the Action: Ensure that the action is registered in your Nova resource. For example, in your College resource:

    use App\Nova\Actions\ViewCollegeAction;
    
    class College extends Resource
    {
        // Other resource methods...
    
        public function actions(Request $request)
        {
            return [
                new ViewCollegeAction,
            ];
        }
    }
    
  4. Ensure the openInNewTab Method Exists: If Action::openInNewTab is not a built-in method, you might need to create a custom response. Here’s an example of how you can create a custom response to open a link in a new tab:

    use Laravel\Nova\Actions\Action;
    use Laravel\Nova\Fields\ActionFields;
    use Illuminate\Support\Collection;
    
    class ViewCollegeAction extends Action
    {
        public function handle(ActionFields $fields, Collection $models)
        {
            $model = $models->first();
            $link = route('colleges.show', ['college' => $model->id]);
    
            return Action::message('Click <a href="' . $link . '" target="_blank">here</a> to view the college.');
        }
    }
    

By following these steps, you should be able to create a Nova action that correctly links to the frontend and allows the admin to view the changes made to a College.

Please or to participate in this conversation.