-
Notifications
You must be signed in to change notification settings - Fork 11.3k
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
[5.5] Add an argument to withTrashed #22888
Conversation
There is already a method public function index(Request $request)
{
return User::query()->when($request->showDeleted, function ($query) {
return $query->withTrashed();
})->get();
} |
Hmmm... I thought it would be better to have 2 methods: |
There is a |
How would this work for everyone that has overwritten the macro and doesn't have the new parameter? Imagine all packages calling Model::withTrashed(false), and only getting trashed items... |
Why would you use |
Dylan, there's a use case in the PR description, and I think it's pretty self-explanatory. |
@sisve, the custom macro would just overwrite the stock one and there will be no collision, but just to prove it let's walk through a process of creating it.
For both these methods let's assume the stock $builder->macro('withTrashed', function (Builder $builder, $argument = true) {
dump('Original macro', $argument);
});
<?php
namespace App\Support;
trait CustomSoftDeletes
{
public static function bootCustomSoftDeletes()
{
static::addGlobalScope(new CustomSoftDeletingScope());
}
} And along with it, the new scope: <?php
namespace App\Support;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class CustomSoftDeletingScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
//
}
public function extend(Builder $builder)
{
$builder->macro('withTrashed', function (Builder $builder) {
dump('Overwritten macro');
});
}
} When a model uses both class Product extends Model
{
use SoftDeletes, CustomSoftDeletes;
//
} Product::withTrashed(); // Overwritten macro
$query = Product::query();
$query->macro('withTrashed', function(Builder $builder) {
dump('Overwritten macro');
});
$query->withTrashed(); Does this answer your question? |
I consider it an anti-pattern to have |
Also:
I think you misunderstood the argument here.
|
I would see it as
|
If you don't want trashed records, you don't call it |
That's why I provided the use case, to show in which situations it's useful. |
I think this would have been better as a new method |
I get your use case but then what this is doing is passing the dirt to the framework code. And this practice will spill on to the other functions as well. |
I cannot think of any reason why this could ever be a good idea. |
@36864 I like your proposal and I understand all the criticism. |
@36864 I also think something like |
I'll spin up a new PR reverting this and adding |
I have created a new PR which reverts this and adds |
Definitely an antipattern. Very strange merge. |
This adds an option to pass an argument to
withTrashed
which decides whether or not to include the trashed records.I believe this doesn't introduce any breaking changes, hence the 5.5 branch.
Use case:
instead of: