How to Check If a (Blade) View File Exists in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to use Carbon in Laravel Blade or Controller File?
- How to Call Controller Function in Blade Laravel?
- Laravel 9 Clear Cache of Route, View, Config, Event Commands
- Laravel Blade Include File If Exists Example
- Laravel Include Blade File with Data Example
- Laravel Blade @unless Directive Example
- How to Use MySQL View in Laravel?
- How to Create Blade File in Laravel using CMD?
- How to Get Current URL in Laravel?
- How to Pass Data to All Views using Composer Share in Laravel?
- How to use Inject View in Laravel?
- Laravel Ajax Render View With Data Example
- Laravel Blade Check if View Exists or Not Example