Laravel How to Check If Blade View File Exists Tutorial

Share this Article
Reading Time: 4 minutes
351 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.

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.

Learn More –

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.

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.

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.

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

Buy Me a Coffee

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.

Was this post helpful?

PHP Web Development Courses on Offer in just $4
PHP Web Development Courses on Offer in just $4