How to check if a (blade) view file exists

Laravel: How to check if a (blade) view file exists

Checking if a blade view file exists is very easy.

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

Or if in a blade view:

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

Some related things to be aware of:

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

  1. @includeIf('', ['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.

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

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

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

webdevetc profile pic

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This ( is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.


Comments and discussion about How to check if a (blade) view file exists

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the Laravel language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to find an item by Primary Key in Eloquent, or throw a ModelNotFoundException exception if it isn't found?

When is __destruct called in PHP?

How to merge/concat two arrays together in JS?

How to run an artisan command from a controller

What is the difference between reference and primitive data types types in Javascript?

How to order a Laravel hasMany relationship?

How to provide a default model object for relationships?

How does PHP compare objects?

How to manually write your own pagination in PHP

How to set up an alias to map one directory to another destination