Hello and do not be tired
Is there a way to check for a blade.php file in Laravel?
Because I want to check if the file exists before addressing
CodePudding user response:
You can try something like this
if(view()->exists($view)){
return view($view)->render();
}
return "404 page not found";
CodePudding user response:
Here how you can get know that file exists or not in Laravel
if (\View::exists('some.view')) {
}
reference from this link https://webdevetc.com/programming-tricks/laravel/laravel-blade/how-to-check-if-a-blade-view-file-exists/
CodePudding user response:
Actually, you don't need this check because it is not a dynamic element. But if there is a reason you need to do it, you can simply use php's own function file_exists("bla.blade.php");. it will return true or false.
CodePudding user response:
To complete the answer of @kamran-khalid you can also include a partial in blade only when the view exists
{{-- include `view.name` only if it exists --}}
@includeIf('view.name', ['status' => 'complete'])
{{-- include the first partial that exists --}}
@includeFirst(['custom.admin', 'admin'], ['status' => 'complete'])
More information in the documentation: https://laravel.com/docs/8.x/blade#including-subviews
