Ryan H
Ryan H

Reputation: 2945

Removing Laravel global scopes via a controller method apiResource

I'm using global scopes in a few of my models. I'd like to be able to dynamically remove the scoped logic at a controller level. My scope is applied to my PingtreeGroup model, and when I try to remove the global scope via my controller show method I get an error.

Too few arguments to function Illuminate\Database\Eloquent\Builder::withoutGlobalScope(), 0 passed

/**
 * Display the specified resource.
 *
 * @return \Illuminate\Http\Response
 */
public function show(Company $company, PingtreeGroup $pingtreeGroup)
{
    $this->authorize('view', $pingtreeGroup);

    $pingtreeGroup = $pingtreeGroup->withoutGlobalScope()->load([
        'pingtree_group_entries',
        'pingtree_group_entries.pingtree' => function ($query) {
            $query->withCount('pingtree_entries');
        },
        'pingtree_group_entries.pingtree.pingtree_entries',
        'pingtree_group_entries.pingtree.pingtree_entries.buyer_tier',
        'pingtree_group_entries.pingtree.pingtree_entries.buyer_tier.buyer',
    ])->loadCount('pingtree_group_entries');

    return new ApiSuccessResponse($pingtreeGroup);
}

What am I missing?

Upvotes: 0

Views: 447

Answers (1)

piscator
piscator

Reputation: 8699

Try to add the class name of the global scope to the withoutGlobalScope() method:

$pingtreeGroup->withoutGlobalScope(MyGlobalScope::class)

You can read more about removing global scopes in the documentation

Upvotes: 1

Related Questions