pmatseykanets / laravel-scout-postgres

PostgreSQL Full Text Search Engine for Laravel Scout
MIT License
159 stars 36 forks source link

Searching model with Soft Delete #23

Open sembrex opened 6 years ago

sembrex commented 6 years ago

I'm using dev-master version of laravel-scout-postgres. Both withTrashed and onlyTrashed method are not properly handled. Scout Builder has __soft_deleted in wheres attribute when its config soft_delete set to true. This will throw an Exception because no such column in the table.

Current PostgresEngine only set $query->whereNull($builder->model->getDeletedAtColumn()); if the model use SoftDeletes.

I modify the engine to deal with this problem.

        // Handle soft deletes
        if (!$this->isExternalIndex($builder->model)) {
            if ($this->usesSoftDeletes($builder->model) && isset($builder->wheres['__soft_deleted'])) {
                if ($builder->wheres['__soft_deleted']) {
                    $query->whereNotNull($builder->model->getDeletedAtColumn());
                } else {
                    $query->whereNull($builder->model->getDeletedAtColumn());
                }
                unset($builder->wheres['__soft_deleted']);
            }
        }

        // Apply where clauses that were set on the builder instance if any
        foreach ($builder->wheres as $key => $value) {
            $query->where($key, $value);
            $bindings->push($value);
        }

        /* Deleted
        // If parsed documents are being stored in the model's table
        if (! $this->isExternalIndex($builder->model)) {
            // and the model uses soft deletes we need to exclude trashed rows
            if ($this->usesSoftDeletes($builder->model)) {
                $query->whereNull($builder->model->getDeletedAtColumn());
            }
        }
        */