Skip to content

Searching model with Soft Delete #23

@sembrex

Description

@sembrex

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());
            }
        }
        */

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions