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

Repository usage with soft-deletion #30

Open
xavierpantet opened this issue Jun 19, 2020 · 0 comments
Open

Repository usage with soft-deletion #30

xavierpantet opened this issue Jun 19, 2020 · 0 comments

Comments

@xavierpantet
Copy link

Hi,

I am using Laravel built-in soft-deletion feature for some model and would like to create the withTrashed scope on my repository.

Best I could do so far is this:

public function allWithTrashed($columns = ['*'])
{
    $this->newQuery();
    return $this->query->withoutGlobalScope(SoftDeletingScope::class)->get($columns);
}

which is OK but I don't really like it because it is a "terminal" method (like all(), find(), ...) whereas I need it to be used more like a scope so that I could write things like $myRepo->withTrashed()->find(...).

I also naively tried this approach:

public function scopeWithTrashed()
{
    $this->addScopeQuery(function ($query){
        return $query->whereNotNull('deleted_at');
    });
}

Obviously it always returns me an empty result set because the generated SQL query contains a filter of the form WHERE deleted_at IS NULL AND deleted_at IS NOT NULL.

Moreover, I tried this:

public function scopeWithTrashed()
{
    $this->newQuery();
    $this->query = $this->query->withoutGlobalScope(SoftDeletingScope::class);
    return $this;
}

Which is exactly what I need but it does not work because it looks like SoftDeletingScope::class is re-added automatically at some point.

Is there any way I can make this last example work?

Thank you in advance and best!

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

1 participant