ItSolutionStuff.com

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

By Hardik Savani • April 16, 2024
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: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to use Carbon in Laravel Blade or Controller File?

Read Now →

How to Call Controller Function in Blade Laravel?

Read Now →

Laravel 9 Clear Cache of Route, View, Config, Event Commands

Read Now →

Laravel Blade Include File If Exists Example

Read Now →

Laravel Include Blade File with Data Example

Read Now →

Laravel Blade @unless Directive Example

Read Now →

How to Use MySQL View in Laravel?

Read Now →

How to Create Blade File in Laravel using CMD?

Read Now →

How to Get Current URL in Laravel?

Read Now →

How to Pass Data to All Views using Composer Share in Laravel?

Read Now →

How to use Inject View in Laravel?

Read Now →

Laravel Ajax Render View With Data Example

Read Now →

Laravel Blade Check if View Exists or Not Example

Read Now →