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

You cannot user Where filter with a different name vs column. #6

Open
bbrala opened this issue Jul 27, 2021 · 3 comments
Open

You cannot user Where filter with a different name vs column. #6

bbrala opened this issue Jul 27, 2021 · 3 comments

Comments

@bbrala
Copy link

bbrala commented Jul 27, 2021

When configuring a filter as follows:

Where::make('modified_since', 'updated_at')->using('>='),

The generator breaks, it tries to get example data from the name of the filter, ignoring the column.

@bbrala
Copy link
Author

bbrala commented Jul 27, 2021

We could do something very ugly in the Where::filter

/** @var \Illuminate\Database\Eloquent\Builder $builder */
        $builder = $this->route->schema()::model()::query();
        $query = $builder->getQuery();
        $probableColumn = $builder->qualifyColumn($key);
        $this->filter->apply($builder, 'appliedWhere');

        foreach($builder->getQuery()->wheres as $where) {
          if ($where['value'] === 'appliedWhere' && $where['column'] !== $probableColumn) {
            $column = $where['column'];
          }
        }

But perhaps its just better to make sure there is no empty ecamples:

 $examples = collect($this->generator->resources()
          ->resources($this->route->schema()::model()))
          ->pluck($column)
          ->filter()
          ->map(function ($f) {
              // @todo Watch out for ids?
                 return Example::create($f)->value($f);
          })
          ->toArray();

This also applies to other filters with the following signature public static function make(string $name, string $column = null).

@bbrala
Copy link
Author

bbrala commented Aug 2, 2021

The related pull request (laravel-json-api/eloquent#18) has been merged, the methods are now public and part of the trait "Conceirns\HasColum". This means we can sniff this out and ask for the column name, fixing this bug :)

@captnCC
Copy link

captnCC commented Aug 3, 2021

Very nice, gonna implement it in the next days!

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