How to Check If a (Blade) View File Exists in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

Hi Dev,

This article will provide an example of laravel check if blade file exists. I would like to share with you laravel check if view file exists. I explained simply step by step how to check blade file exists in laravel. we will help you to give an example of how to check file exists in laravel. Let's see below example how to check view file exists or not in laravel.

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.

We can use View facade and helper to check blade or view file exists or not in laravel. we can check blade file exists or not in controller and blade file as well. let's see the below examples:

Example 1: Check using Helper in Controller

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$title = "Welcome to ItSolutionStuff.com";

if(view()->exists('admin.demo')){

return view('admin.demo', compact('title'));

}

}

}

Example 2: Check using Facade in Controller

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use View;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$title = "Welcome to ItSolutionStuff.com";

if(View::exists('admin.demo')){

return view('admin.demo', compact('title'));

}

}

}

Example 3: Check using Helper in Blade

Blade File:

@if(view()->exists('admin.demo'))

{{-- Blade File is Exists --}}

@else

{{-- Blade File is not Exists --}}

@endif

Example 4: Check using Facade in Blade

Blade File:

@if(View::exists('admin.demo'))

{{-- Blade File is Exists --}}

@else

{{-- Blade File is not Exists --}}

@endif

I hope it can help you...

Tags :
Shares