public static function areRelated(...$relations)
	{
		try {
			$max_key = count($relations) - 1;

			foreach ($relations as $key => $current) {
				if (!is_array($current)) {
					$previous = null;

					if ($key > 0) {
						$previous = $relations[ $key - 1 ];
						$previous = is_array($previous)
							? $previous[ 0 ] : $previous;
					}

					$basename = strtolower(class_basename($current));
					$method   = Str::plural($basename);

					if (!is_null($previous)) {
						if (!method_exists($previous, $method)) {
							$method = Str::singular($basename);
						}

						if (!method_exists($previous, $method)) {
							throw new \Exception('UNABLE_TO_FIND_RELATIONSHIP');
						}
					}

					$relations[ $key ] = [ $current, $method ];
				}

				if (!($relations[ $key ][ 0 ] instanceof Model)) {
					throw new \InvalidArgumentException('INVALID_MODEL');
				}
			}

			foreach ($relations as $key => $current) {
				if ($key !== $max_key) {
					$model    = $current[ 0 ];
					$relation = $relations[ $key + 1 ];

					$model->{$relation[ 1 ]}()->findOrFail($relation[ 0 ]->id);
				}
			}

			return true;
		} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
			return false;
		}
	}
public static function areRelated(...$relations)
	{
		try {
			$max_key = count($relations) - 1;

			foreach ($relations as $key => $current) {
				if (!is_array($current)) {
					$previous = null;

					if ($key > 0) {
						$previous = $relations[ $key - 1 ];
						$previous = is_array($previous)
							? $previous[ 0 ] : $previous;
					}

					$basename = strtolower(class_basename($current));
					$method   = Str::plural($basename);

					if (!is_null($previous)) {
						if (!method_exists($previous, $method)) {
							$method = Str::singular($basename);
						}

						if (!method_exists($previous, $method)) {
							throw new \Exception('UNABLE_TO_FIND_RELATIONSHIP');
						}
					}

					$relations[ $key ] = [ $current, $method ];
				}

				if (!($relations[ $key ][ 0 ] instanceof Model)) {
					throw new \InvalidArgumentException('INVALID_MODEL');
				}
			}

			foreach ($relations as $key => $current) {
				if ($key !== $max_key) {
					$model    = $current[ 0 ];
					$relation = $relations[ $key + 1 ];

					$model->{$relation[ 1 ]}()->findOrFail($relation[ 0 ]->id);
				}
			}

			return true;
		} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
			return false;
		}
	}
public static function areRelated ( ... $ relations ) { try { $ max_key = count ( $ relations ) - 1 ; foreach ( $ relations as $ key => $ current ) { if ( ! is_array ( $ current ) ) { $ previous = null ; if ( $ key > 0 ) { $ previous = $ relations [ $ key - 1 ] ; $ previous = is_array ( $ previous ) ? $ previous [ 0 ] : $ previous ; } $ basename = strtolower ( class_basename ( $ current ) ) ; $ method = Str :: plural ( $ basename ) ; if ( ! is_null ( $ previous ) ) { if ( ! method_exists ( $ previous , $ method ) ) { $ method = Str :: singular ( $ basename ) ; } if ( ! method_exists ( $ previous , $ method ) ) { throw new \ Exception ( 'UNABLE_TO_FIND_RELATIONSHIP' ) ; } } $ relations [ $ key ] = [ $ current , $ method ] ; } if ( ! ( $ relations [ $ key ] [ 0 ] instanceof Model ) ) { throw new \ InvalidArgumentException ( 'INVALID_MODEL' ) ; } } foreach ( $ relations as $ key => $ current ) { if ( $ key !== $ max_key ) { $ model = $ current [ 0 ] ; $ relation = $ relations [ $ key + 1 ] ; $ model -> { $ relation [ 1 ] } ( ) -> findOrFail ( $ relation [ 0 ] -> id ) ; } } return true ; } catch ( \ Illuminate \ Database \ Eloquent \ ModelNotFoundException $ e ) { return false ; } }
Check if any number of models are related to each other

@param Model[]|array[] $relations
@throws \InvalidArgumentException|\Exception
@return bool
Check if any number of models are related to each other