Example of Global scopes in Senither/ShareX-Media-Server

File app/Scopes/UserScope.php (link to Github)
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class UserScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        if (auth()->check()) {
            $builder->where(
                'user_id',
                session()->has('imposter_id')
                    ? session()->get('imposter_id')
                    : request()->user()->id
            );
        }
    }
}
File app/Traits/BelongsToUser.php (link to Github)
use App\Scopes\UserScope;

trait BelongsToUser
{
    public static function bootBelongsToUser()
    {
        static::addGlobalScope(new UserScope());
    }
}
File app/Models/Url.php (link to Github)
use App\Traits\BelongsToUser;
use Illuminate\Database\Eloquent\Model;

class Url extends Model
{
    use BelongsToUser;
    //
}

Additional resources on global scopes: