How to Check if a Blade File Exists in Laravel?

Nov 22, 2022

.

Admin

How to Check if a Blade File Exists in Laravel?

Hello Friends,

Here, I will show you how to check if a blade file exists in laravel. step by step explain laravel and check if a blade file exists example. if you want to see an example of laravel check if a blade file exists then you are in the right place. I explained simply about laravel check if the blade file exists with an example. Here, Creating a basic example of checking for file existence of the blade template in laravel.

You can use this example with the versions of laravel 6, laravel 7, laravel 8, and laravel 9.

You have just to follow the below step and you will get the layout as below:

Step 1: Install Laravel


This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create Format Route

web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\BladeExistsController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('/index',[BladeExistsController::class, 'index']);

Step 3: create BladeExistsController

we will create BladeExistsController. so you can see the below code with output:

php artisan make:controller BladeExistsController

App\Http\Controllers\BladeExistsController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class BladeExistsController extends Controller

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function index()

{

if(\View::exists('welcome')){

dd('exists');

}else{

dd('not exists');

}

}

}

Step 4: Start Development Server

Start the development server. Use the PHP artisan serve command and start your server:

php artisan serve

Now you are ready to run our example so run the below command to quick run.

http://localhost:8000/index

Output:

"exists" // app/Http/Controllers/BladeExistsController.php:18

I hope it can help you...

#Laravel