Check If There Is Folder Exists In Laravel

For many purposes, it requires checking for the specific directory in the Laravel project. You can use the IF-ELSE statement to check and the certain action to be executed.

@if (!File::isDirectory('storage/images/default'))
    <a href="{{url('/images')}}">Hit here</a>
@endif

Let’s get an example

Suppose, you need to check if there is a folder present in the public folder. Then symlink is to be created, and the link disappears.

In this case, you check the public folder for the storage folder and if it is not there, a link will appear and when the task is done the link will be disappeared.

@if (!File::isDirectory('storage/images/default'))
    <a href="{{url('/images')}}">Hit here</a>
@endif

Above line in the blade which checks if there are images/default folder in public. If not then the link appears. When you hit the link, the below function will be called.

Route.php file below:

Route::get('/images', function() {
    Artisan::call('storage:link');

    return redirect()->back()->withMessage('Successfully created image storage folder.');
});

Related Posts