How To Check If A Blade File Exists In Laravel?

Hello artisan,

In this laravel check if blade file exists tutorial, I will show you how we can check inside a laravel controller and also inside blade file whether a blade view file exists or not in laravel. If you don't know how to write condition of laravel check if blade file exists then this example is for you. Laravel provides View::exists() or view()->exists() method to check whether a blade view file exists or not in laravel.

We can use this source code with laravel 6, laravel 7, laravel 8 and laravel 9 versions. We can use View facade or view() helper to check blade or view file exists or not in laravel. Let's see the below examples:

app/Http/Controllers/DemoController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function index()
    {
        if (view()->exists('home'))
        {
            return view('home');
        }
    }
}

 

Check inside blade file:

@if(view()->exists('admin.demo'))
    {{-- Blade File is Exists --}}
@else
    {{-- Blade File is not Exists --}}
@endif

 

Read also: Different Ways of Passing Data From Controller to View in Laravel

 

Hope it can help you.

 

#laravel