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('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.

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

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

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

webdevetc profile pic
webdevetc

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



More...


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 do comments in Blade files?

How to find the 2nd most common item in an array?

How to force non www in htaccess

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

How to select what columns to return when calling ::all()

How to replace whitespace with a single space

How to disable file hotlinking in Apache with .htaccess

How to show a specific view file for certain error codes?

What is snake case?

How to add and remove items to an array in JS?