[Vuejs]-Retrieving multiple files(images) from storage in Laravel

0👍

TL:DR

picPath attribute should contain the actual file path

I am sharing you a snippet which I used to manage my files

    $auth_id = 0;
    $storage_details = [];

    if (auth()->check()) {
        $auth_id = auth()->user()->id;
    }

    $file_name = $auth_id . '_' . date('d-m-Y_H-i-s') . '.' . $file->getClientOriginalExtension();

    $store = $this->getStorage()->putFileAs('records', $file, $file_name);
    if (!$store) {
        return $storage_details;

    } else {

        $storage_details['file_path'] = $this->getStorage()->getDriver()->getAdapter()->getPathPrefix() . 'records' . DIRECTORY_SEPARATOR . $file_name;
        $storage_details['file_name'] = $file_name;
        $storage_details['driver'] = $this->getStorage()->getDriver();
        $storage_details['disk'] = $this->getDisk();
        $storage_details['relative_path'] = 'records' . DIRECTORY_SEPARATOR . $file_name;

        return $storage_details;
    }

And here is how I am saving them to DB

    $task = new self();
    $task->id = $task_id;
    $task->user_id = auth()->user()->id;
    $task->type = 'process_records';

    $meta = [];
    if (array_key_exists('file_path', $storage_details)) {
        $meta['file_path'] = $storage_details['file_path'];
    }
    if (array_key_exists('file_name', $storage_details)) {
        $meta['file_name'] = $storage_details['file_name'];
    }
    if (array_key_exists('driver', $storage_details)) {
        $meta['driver'] = $storage_details['driver'];
    }
    if (array_key_exists('disk', $storage_details)) {
        $meta['disk'] = $storage_details['disk'];
    }
    if (array_key_exists('relative_path', $storage_details)) {
        $meta['relative_path'] = $storage_details['relative_path'];
    }

    $task->meta = $meta;
    $task->save();

Now All you need is to iterate over the relative path and if in future you change your driver to AWS or something else you can easily implement logic

Leave a comment