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

feat: upgrade seach #475

Merged
merged 3 commits into from
Sep 26, 2023
Merged

Conversation

k-rustemuly
Copy link
Contributor

@k-rustemuly k-rustemuly commented Sep 15, 2023

 public function search(): array
    {
        return [
            'id',
            'user' => [
                'iin',
                'name'
            ]
        ];
    }

Можно делать поиск по отношениям

@lee-to
Copy link
Collaborator

lee-to commented Sep 15, 2023

Как тебе идея добавить проверку есть ли такое отношение, если есть то твоя логика а если нет то искать в json

@lee-to
Copy link
Collaborator

lee-to commented Sep 15, 2023

И еще можно сделать возможность указывать через точку user.name а потом через Arr делать undot

@lee-to
Copy link
Collaborator

lee-to commented Sep 15, 2023

пример

foreach ($this->search() as $key => $column) {
  if (is_string($column) && str($column)->contains('.')) {
      $column = str($column)
          ->explode('.')
          ->tap(function (Collection $data) use (&$key) {
              $key = $data->first();
          })
          ->slice(-1)
          ->values()
          ->toArray();
  }
  
  if (is_array($column)) {
      $builder->when(
          method_exists($this->getModel(), $key),
          fn (Builder $query) => $query->orWhereHas(
              $key,
              fn (Builder $q) => collect($column)->each(fn ($item) => $q->where(
                  fn (Builder $qq) => $qq->orWhere($item, 'LIKE', "%$terms%")
              ))
          ),
          fn (Builder $query) => collect($column)->each(fn ($item) => $query->where(
              fn (Builder $qq) => $qq->orWhereJsonContains("$key->$item", $terms)
          ))
      );
  } else {
      $builder->orWhere($column, 'LIKE', "%$terms%");
  }
  }

@k-rustemuly
Copy link
Contributor Author

изучаю тесты

@lee-to lee-to self-requested a review September 20, 2023 10:03
@lee-to lee-to merged commit 00c8dd8 into moonshine-software:1.5.x Sep 26, 2023
2 of 5 checks passed
@k-rustemuly k-rustemuly deleted the search-by-relation branch December 19, 2023 14:34
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

Successfully merging this pull request may close these issues.

2 participants