throw_unless

The throw_unless function throws the given exception if a given boolean expression evaluates to false:
<?php throw_unless(Auth::user()->isAdmin(), AuthorizationException::class);

throw_unless(
    Auth::user()->isAdmin(),
    AuthorizationException::class,
    'You are not allowed to access this page.'
);
<?php         preg_match('/(?:\n\s*|^\s*)<([a-zA-Z0-9\-]+)/', $html, $matches, PREG_OFFSET_CAPTURE);



        throw_unless(

            count($matches),

            new RootTagMissingFromViewException

        );



        $tagName = $matches[1][0];

        $lengthOfTagName = strlen($tagName);
<?php         collect($rules)

            ->keys()

            ->each(function($ruleKey) use ($data) {

                throw_unless(

                    array_key_exists(Utils::beforeFirstDot($ruleKey), $data),

                    new \Exception('No property found for validation: ['.$ruleKey.']')

                );

            });

    }
<?php     ) {}



    public static function getKey() {

        throw_unless(

            property_exists(static::class, 'key'),

            new \Exception('You need to define static $key property on: '.static::class)

        );



        return static::$key;

    }
<?php     {

        $component = '\Illuminate\Console\View\Components\\'.ucfirst($method);



        throw_unless(class_exists($component), new InvalidArgumentException(sprintf(

            'Console component [%s] not found.', $method

        )));



        return with(new $component($this->output))->render(...$parameters);

    }
<?php     {

        $this->expectException(LogicException::class);



        throw_unless(false, new LogicException);

    }



    public function testThrowUnlessDefaultException()
<?php     {

        $this->expectException(RuntimeException::class);



        throw_unless(false);

    }



    public function testThrowUnlessExceptionWithMessage()
<?php         $this->expectException(RuntimeException::class);

        $this->expectExceptionMessage('test');



        throw_unless(false, 'test');

    }



    public function testThrowUnlessExceptionAsStringWithMessage()
<?php         $this->expectException(LogicException::class);

        $this->expectExceptionMessage('test');



        throw_unless(false, LogicException::class, 'test');

    }



    public function testThrowReturnIfNotThrown()
<?php     public function testThrowReturnIfNotThrown()

    {

        $this->assertSame('foo', throw_unless('foo', new RuntimeException));

    }



    public function testThrowWithString()
<?php     public function ensureExists()

    {

        throw_unless($this->exists(), new ComposerLockFileNotFoundException(Path::makeRelative($this->path)));



        return $this;

    }
<?php     {

        $structure = $collection->structure();



        throw_unless($structure, new NotFoundHttpException("Collection [{$collection->handle()}] is not a structured collection"));



        $site = $this->queryParam('site');

        $tree = $structure->in($site);
<?php         $site = $this->queryParam('site');

        $tree = $structure->in($site);



        throw_unless($tree, new NotFoundHttpException("Collection [{$collection->handle()}] not found in [{$site}] site"));



        return $tree;

    }
<?php     {

        $nav = Nav::find($handle);



        throw_unless($nav, new NotFoundHttpException("Navigation [{$handle}] not found"));



        $site = $this->queryParam('site');

        $tree = $nav->in($site);
<?php         $site = $this->queryParam('site');

        $tree = $nav->in($site);



        throw_unless($tree, new NotFoundHttpException("Navigation [{$handle}] not found in [{$site}] site"));



        return $tree;

    }
<?php     {

        $user = User::find($id);



        throw_unless($user, new NotFoundHttpException("User [$id] not found."));



        return $user;

    }