How to check if a (blade) view file exists

October 26, 2018

Checking if a blade view file exists is very easy.

if (\View::exists('some.view')) { ... }

Or if in a blade view:

@if(\View::exists('some.view'))
  ...
@endif      

Some related things to be aware of:

@includeIf - include a view from within blade, if the view exists

@includeIf('view.name', ['some' => 'data'])

@includeFirst - Include the first view that exists

(the final one would be the default shown if no others exist).

In this example it would try and see if custom.admin existed (/resources/views/custom/admin.blade.php), but if it didn't then it would try admin.

@includeFirst(['custom.admin', 'admin'], ['some' => 'data'])

@includeWhen - Used to include a view when a boolean condition is true

@includeWhen($boolean, 'view.name', ['some' => 'data'])