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 group a SQL statement in brackets/parenthesis

What are some ways you can log debug messages and data in JS?

How to namespace a Laravel route group?

How to install and use jpegoptim on Linux CentOS

What is camel case?

What are generators in PHP, and how do they compare to arrays?

How to block IP addresses in Apache by using regular expressions (regex) in .htaccess

How to select all rows for a certain month (or day, year or time), using Eloquent

What is Agile software development?

How to force gzip compression in Apache with .htaccess