Laravel How to Check If Blade View File Exists Tutorial

Reading Time: 4 minutes
1,010 Views

In this tutorial we will see How to work with Laravel Blade Check if View Exists. Article will be very interesting to learn and easy to implement into application.

View file is the file which presents the application interface to users. Users interact via application views. In laravel, view files are called as blade template files.

Read More: Remove HTML Tags From String Tutorial in CodeIgniter 4

We search view file in laravel in two ways: Using view helper and Using View facade. Here, we will see both ways to check blade template file existence. You can search laravel view file either in main folder or within subfolders.

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Methods To Check Blade (View) Template File

There are two ways in this article gives you the understanding of blade view file to check if it exists or not:

  • Using view() Helper
  • Using View Facade

We will see step by step both ways.

Read More: Laravel How To Remove HTML Tags From String Tutorial

Using view() Helper Function

Consider a view file with name demo.blade.php which exists inside /resources/views folder.

In controller to check this view file as,

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller
{
    public function checkViewFile()
    {
        if (view()->exists('demo')) {

            echo "Exists";
        } else {

            echo "Not Exists";
        }
    }
}

view()->exists(‘demo’) this line is looking for demo.blade.php view file inside /resources/views folder of application.

Blade (View) Template File inside Subfolder

The same view file i.e demo.blade.php, consider inside /resources/views/admin folder.

In controller to check this view file as,

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller
{
    public function checkViewFile()
    {
        if (view()->exists('admin.demo')) {

            echo "Exists";
        } else {

            echo "Not Exists";
        }
    }
}

view()->exists(‘admin.demo’) this line is looking for demo.blade.php view file inside /resources/views/admin folder of application.

Read More: Laravel 9 How To Parse Date Format Example Tutorial

Using View Facade Method

Consider another view file with name users.blade.php which exists inside /resources/views folder.

In controller to check this view file as,

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;

class DemoController extends Controller
{
    public function checkViewFile()
    {
        if (View::exists('users')) {

            echo "Exists";
        } else {
            
            echo "Not Exists";
        }
    }
}

View::exists(‘users’) this line is looking for users.blade.php view file inside /resources/views folder of application.

Blade (View) Template File inside Subfolder

The same view file i.e users.blade.php, consider inside /resources/views/site folder.

In controller to check this view file as,

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;

class DemoController extends Controller
{
    public function checkViewFile()
    {
        if (View::exists('site.users')) {

            echo "Exists";
        } else {
            
            echo "Not Exists";
        }
    }
}

View::exists(‘site.users’) this line is looking for users.blade.php view file inside /resources/views/site folder of application.

Read More: Laravel 9 Count Working Days Between Two Dates Example

We hope this article helped you to learn Laravel How to Check If Blade View File Exists Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more