Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access Filter Values In Fields Function #80

Open
MannikJ opened this issue Mar 4, 2020 · 1 comment
Open

Access Filter Values In Fields Function #80

MannikJ opened this issue Mar 4, 2020 · 1 comment

Comments

@MannikJ
Copy link

MannikJ commented Mar 4, 2020

Consider the following nova lense:

<?php

namespace App\Nova\Lenses;

use Illuminate\Http\Request;
use Laravel\Nova\Http\Requests\LensRequest;
use Laravel\Nova\Fields\Number;
use Maatwebsite\LaravelNovaExcel\Actions\DownloadExcel;
use App\Nova\Filters\Inventory\Date;

class Inventory extends Lens;
{
    public function fields(Request $request)
    {
       $date = ??? // Access the date filter value
        return [
            Number::make('Stock', 'stock')->resolveUsing(function () use ($date) {
                ...
                return $stock;
            }),
        ];
    }

    public function filters(Request $request)
    {
        return [
            new Date,
        ];
    }

    public function actions(Request $request)
    {
        return [
            (new DownloadExcel)->withHeadings()
        ];
    }
}

If the fields function is called by nova itself to form the index results the request object contains the active filters values. However if it is called from the DownloadExcel action, they are empty.
I think this could be considered a bug. Or at least I would say it should be possible to access the filter values from within the fields function

Is there at least a quick workaround to get the filter values?

Please also see laravel/nova-issues#2405

@oleksandr-roskovynskyi
Copy link

This should help you:

public function calculate(NovaRequest $request)
    {
        $model = PaymentSystem::make();

        if ($request->has('filters')) {
            // Get the decoded list of filters
            $filters = json_decode(base64_decode($request->filters));

            foreach ($filters as $filter) {
                if (empty($filter->value)) {
                    continue;
                }

                // Create a new instance of the filter and apply the query to your model
                $model = (new $filter->class)->apply($request, $model, $filter->value);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants