Laravel

laravel Storage file save delete show helper

In Laravel, managing files and storage is an essential aspect of many web applications. The Laravel Storage system offers a powerful way to handle file upload, storage, retrieval, and deletion. However, the process can sometimes become convoluted, especially when dealing with multiple files and ensuring proper management. In this blog post, we will explore how to streamline these operations using custom helper functions, making your file management tasks in Laravel more efficient and organized.

Creating Custom Helper Functions

The first step to simplifying your file storage operations is to create custom helper functions. These functions will encapsulate the common tasks of saving, retrieving, and deleting files, making your code more modular and easier to maintain. Here, we will create two helper functions: `file_path` and `file_delete`.

The `file_path` Function
The `file_path` function will generate URLs or paths for files, taking into account their existence and providing fallback options.

// app\Helpers\Helpers.php
use Illuminate\Support\Facades\Storage;
if (!function_exists('file_path')) {
  function file_path($value, $type = 'url', $default = true)
  {
      if ($value) {
          if (Storage::exists($value)) {
              if ($type === 'url') {
                  return Storage::url($value);
              }
              return Storage::path($value);
          }
      }
      if ($default) {
          return url('placeholder.jpg');
      }
      return null;
  }
}

The `file_delete` Function
The `file_delete` function deletes a file from storage if it exists.

// app\Helpers\Helpers.php
if (!function_exists('file_delete')) {
  function file_delete($value)
  {
      if ($value) {
          if (Storage::exists($value)) {
              return Storage::delete($value);
          }
      }
      return false;
  }
}

Integration with Laravel

Setting Up the Environment
Before diving into using these helper functions, ensure that you have updated your `.env` file:

APP_URL=http://example.com
FILESYSTEM_DRIVER=public

Autoloading the Helper Functions
Add the `Helpers.php` file to the autoload section in your `composer.json` file:

"autoload": {
  "files": [
      "app/Helpers/Helpers.php"
  ],
  "psr-4": {
      "App\\": "app/"
  }
},

Don't forget to run `composer dump-autoload` to reload the autoloader.

Implementing the Helper Functions

Uploading and Saving Files
To save an uploaded file, you can use Laravel's built-in file storage methods:

if ($request->hasFile('image')) {
   $path = $request->file('image')->store('file-part');
}

Updating Files
When updating a file, consider deleting the previous file using the `file_delete` helper function before storing the new one:

if ($request->hasFile('image')) {
   file_delete($user->image);
   $path = $request->file('image')->store('file-part');
}

Displaying Files
Displaying files becomes straightforward using the `file_path` function:

<img src="{{ file_path($user->image) }}" alt="User Image">

Conclusion

Custom helper functions can significantly simplify your Laravel application's file storage, retrieval, and deletion processes. By encapsulating complex operations in these functions, your code becomes more organized, maintainable, and easier to comprehend. Leveraging the power of Laravel's Storage system alongside these custom helpers, you can build a robust and efficient file management system for your web application.

Leave A Comment