@JarekTkaczyk any ideas? I know you wrote an article on this.
Need help writing remove() for global query scope
Well, this was more difficult than I thought.
Here is my scope, the apply works wonderfully.
use Illuminate\Database\Eloquent\ScopeInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class JobStatusScope implements ScopeInterface
{
/**
* Apply scope on the query.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function apply(Builder $builder, Model $model)
{
$builder
->where('active', true)
->whereHas('user', function($q) {
$q->where('account_type', 'company')
->whereNested(function($r) {
$r->where('stripe_active', true)
->orWhereNotNull('subscription_ends_at')
->where('subscription_ends_at', '>', Carbon::now())
->orWhereNotNull('trial_ends_at')
->where('trial_ends_at', '>', Carbon::today());
});
});
}
/**
* Remove scope from the query.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function remove(Builder $builder, Model $model)
{
$query = $builder->getQuery();
//dd($query->wheres);
$columns = ['stripe_active', 'subscription_ends_at', 'active', 'trial_ends_at'];
foreach ((array) $query->wheres as $key => $where) {
if (in_array($where['column'], $columns)) {
unset($query->wheres[$key]);
$query->wheres = array_values($query->wheres);
}
}
}
}
What I tried in my remove() method didn't work.
I have this in a trait file, and attached to my Model
use App\Cable\Traits\Scopes\JobStatusScope;
trait JobStatusTrait {
public static function bootJobStatusTrait()
{
static::addGlobalScope(new JobStatusScope);
}
/**
* Get the query builder without the scope applied.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function withAll()
{
return with(new static)->newQueryWithoutScope(new JobStatusScope);
}
}
When I try to run MyModel::withAll()->get() it still returns the same filtered data.
When I dd($query->wheres) in the remove() and call the ->withAll() I get this
array:3 [▼
0 => array:3 [▼
"type" => "Null"
"column" => "jobs.deleted_at"
"boolean" => "and"
]
1 => array:5 [▼
"type" => "Basic"
"column" => "active"
"operator" => "="
"value" => true
"boolean" => "and"
]
2 => array:5 [▼
"type" => "Basic"
"column" => Expression {#478 ▼
#value: "(select count(*) from `users` where `jobs`.`user_id` = `users`.`id` and `account_type` = ? and (`stripe_active` = ? or `subscription_ends_at` is not null and `subscription_ends_at` > ? or `trial_ends_at` is not null and `trial_ends_at` > ?) and `users`.`deleted_at` is null)"
}
"operator" => ">="
"value" => Expression {#476 ▶}
"boolean" => "and"
]
]
But even when their subscription expires, I still want them, or me (site staff for that matter) to be able to see/manage the jobs in their/our dashboard even though the jobs will not be visible on the front end of the site.
So this is not a global scope. It is just a regular scope.
Please or to participate in this conversation.