How to Remove global scope from relation in Laravel

✔ Recommended Answer

never mind i just created a new relation

public function getDetalesWithout(){    return $this->hasMany('App\Vouchers_detale','vouchers_detale_voucher_id','id')->withTrashed()->withoutGlobalScope('App\Scopes\getVouchers');}



Answered By: Awar Pulldozer

 Method 2:

In Laravel, you can remove the global scope from a relation by using the withoutGlobalScope() method. This method allows you to remove a global scope from a specific query.

Here's an example of how to remove a global scope from a relation:

$user = User::withoutGlobalScope('active')->find(1); $posts = $user->posts()->withoutGlobalScope('published')->get();

In this example, we have a User model with a posts relation. We want to remove the active global scope from the user, and the published global scope from the user's posts.

The withoutGlobalScope() method takes the name of the global scope as its argument. So, we can use it to remove the global scopes we want to exclude.

By using the withoutGlobalScope() method, we can modify a specific query to exclude certain global scopes, while leaving them intact for other queries.


Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755