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 destroy or remove a cookie in PHP

How do you run a PHP script from the command line?

What does PEAR stand for?

How to get the first row that matches some where queries, or create it if it doesn't exist (in Laravel's Eloquent)?

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

What are the SPL data structures

Force trailing slash (redirect if no trailing slash exists) in .htaccess

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

How do Invokable Controllers work in Laravel?

How to see all of the options for an artisan command?