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     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;

    }
<?php {

    public function update(Request $request, $user)

    {

        throw_unless($user = User::find($user), new NotFoundHttpException);



        $this->authorize('editPassword', $user);
<?php     public function edit(Request $request, $user)

    {

        throw_unless($user = User::find($user), new NotFoundHttpException);



        $this->authorize('edit', $user);
<?php     public function update(Request $request, $user)

    {

        throw_unless($user = User::find($user), new NotFoundHttpException);



        $this->authorize('edit', $user);
<?php     public function destroy($user)

    {

        throw_unless($user = User::find($user), new NotFoundHttpException);



        if (! $user = User::find($user)) {

            return $this->pageNotFound();
<?php         $asset = AssetContainer::find($container)->asset($path);



        throw_unless($asset, new NotFoundHttpException);



        return $this->createResponse($this->generateBy('asset', $asset));

    }
<?php             return $this;

        }



        throw_unless($this->isLivePreview(), new NotFoundHttpException);



        $this->headers['X-Statamic-Draft'] = true;
<?php             return $this;

        }



        throw_unless($this->isLivePreview(), new NotFoundHttpException);



        $this->headers['X-Statamic-Private'] = true;
<?php             $mime = $this->asset->mimeType();

        } else {

            $path = public_path($this->path);

            throw_unless(File::exists($path), new FlysystemFileNotFoundException($path));

            $mime = File::mimeType($path);

        }
<?php     public function ray($value)

    {

        throw_unless(function_exists('ray'), new \Exception('Ray is not installed. Run `composer require spatie/laravel-ray --dev`'));



        ray($value);