<commit_before><?php namespace AuraIsHere\LaravelMultiTenant;

use TenantScope;
use Illuminate\Support\Facades\Config;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use AuraIsHere\LaravelMultiTenant\Exceptions\ModelNotFoundForTenantException;

/**
 * Class ScopedByTenant
 *
 * @package AuraIsHere\LaravelMultiTenant
 *
 * @method static void addGlobalScope(\Illuminate\Database\Eloquent\ScopeInterface $scope)
 * @method static void observe(object $class)
 */
trait ScopedByTenant {

	public static function bootScopedByTenant()
	{
		// Add the global scope that will handle all operations except create()
		static::addGlobalScope($this->getTenantScope());

		// Add an observer that will automatically add the tenant id when create()-ing
		static::observe(new TenantObserver);
	}

	/**
	 * Returns a new builder without the tenant scope applied.
	 *
	 *     $allUsers = User::allTenants()->get();
	 *
	 * @return \Illuminate\Database\Eloquent\Builder
	 */
	public static function allTenants()
	{
		return with(new static)->newQueryWithoutScope($this->getTenantScope());
	}

	/**
	 * Override the default findOrFail method so that we can rethrow a more useful exception.
	 * Otherwise it can be very confusing why queries don't work because of tenant scoping issues.
	 *
	 * @param       $id
	 * @param array $columns
	 *
	 * @throws ModelNotFoundForTenantException
	 */
	public static function findOrFail($id, $columns = array('*'))
	{
		try
		{
			return parent::findOrFail($id, $columns);
		}

		catch (ModelNotFoundException $e)
		{
			throw with(new ModelNotFoundForTenantException())->setModel(get_called_class());
		}
	}

	/**
	 * Returns tenant scope for this model.
	 *
	 * @return Illuminate\Database\Eloquent\ScopeInterface
	 */
	protected function getTenantScope()
	{
		return TenantScopeFacade::getFacadeRoot();
	}	
} <commit_msg>add static getTenantScope method to model<commit_after><?php namespace AuraIsHere\LaravelMultiTenant;

use Illuminate\Support\Facades\Config;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use AuraIsHere\LaravelMultiTenant\Exceptions\ModelNotFoundForTenantException;

/**
 * Class ScopedByTenant
 *
 * @package AuraIsHere\LaravelMultiTenant
 *
 * @method static void addGlobalScope(\Illuminate\Database\Eloquent\ScopeInterface $scope)
 * @method static void observe(object $class)
 */
trait ScopedByTenant {

	public static function bootScopedByTenant()
	{
		// Add the global scope that will handle all operations except create()
		static::addGlobalScope(static::getTenantScope());

		// Add an observer that will automatically add the tenant id when create()-ing
		static::observe(new TenantObserver);
	}

	/**
	 * Returns a new builder without the tenant scope applied.
	 *
	 *     $allUsers = User::allTenants()->get();
	 *
	 * @return \Illuminate\Database\Eloquent\Builder
	 */
	public static function allTenants()
	{
		return with(new static)->newQueryWithoutScope(static::getTenantScope());
	}

	/**
	 * Override the default findOrFail method so that we can rethrow a more useful exception.
	 * Otherwise it can be very confusing why queries don't work because of tenant scoping issues.
	 *
	 * @param       $id
	 * @param array $columns
	 *
	 * @throws ModelNotFoundForTenantException
	 */
	public static function findOrFail($id, $columns = array('*'))
	{
		try
		{
			return parent::findOrFail($id, $columns);
		}

		catch (ModelNotFoundException $e)
		{
			throw with(new ModelNotFoundForTenantException())->setModel(get_called_class());
		}
	}

	/**
	 * Returns tenant scope for this model.
	 *
	 * @return Illuminate\Database\Eloquent\ScopeInterface
	 */
	protected static function getTenantScope()
	{
		return TenantScopeFacade::getFacadeRoot();
	}	
} 
{'fraction_non_alphanumeric': 0.10589812332439678, 'fraction_numerical': 0.0, 'mean_word_length': 4.462664714494876, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4735177', 'n_tokens_mistral': 1171, 'n_tokens_neox': 1106, 'n_words': 288}