developer-unijaya/laravel-uploaded-file

Laravel Uploaded File


Keywords
laravel, developer-unijaya, laravel-uploaded-file
License
MIT

Documentation

Laravel Uploaded File

Latest Version on Packagist Total Downloads

Installation

You can install the package via composer:

composer require developer-unijaya/laravel-uploaded-file

You can run the migrations to create a table - uploaded_files. It will rename existing table into uploaded_files_old for archive:

php artisan migrate

Usage

In your own model, insert the hasUploadedFile trait

<?php
 
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use DeveloperUnijaya\LaravelUploadedFile\Http\Traits\HasUploadedFile;
 
class User extends Model
{
   use HasUploadedFile;
}

To Upload The File inside Model (Single, Multiple File)

<?php
 
namespace App\Http\Controllers;
 
use DeveloperUnijaya\LaravelUploadedFile\FileHelper;
use Illuminate\Http\Request;
use App\Models\User;
 
class UserController extends Controller
{
    /**
     * Upload File using FileHelper
     */
    public function uploadFile(Request $request)
    {   
        $user = User::find($request->id);

        //Single Upload File with overwrite existing file
        if($request->example_upload_one_file){

            //This check if certain document already exists, deleted it.
            if($user->uploadedFiles('example_upload_one_file')->count() >= 1){
                foreach($user->uploadedFiles('example_upload_one_file')->get() as $goodbyefile){
                    FileHelper::deleteFile($goodbyefile->path);
                    $goodbyefile->delete();
                }
            }

            //Upload Files. Refer inside vendor/developer-unijaya/laravel-uploaded-file/FileHelper to understand parameter
            $success = FileHelper::uploadFile($user, ['files' => $request->example_upload_one_file], "example_upload_one_file");
            //error checker
            if($success != "success"){
                return response()->json(['title' => 'Gagal', 'status' => 'error', 'detail' => $success],422);
            }
        }
        //End Single Upload File with overwrite existing file

        //Multiple Upload File with overwrite existing files
        if($request->example_upload_multiple_file){

            //This check if certain document already exists, deleted it.
            if($user->uploadedFiles('example_upload_multiple_file')->count() >= 1){
                foreach($user->uploadedFiles('example_upload_multiple_file')->get() as $goodbyefile){
                    FileHelper::deleteFile($goodbyefile->path);
                    $goodbyefile->delete();
                }
            }

            //Upload Files. Refer inside vendor/developer-unijaya/laravel-uploaded-file/FileHelper to understand parameter
            $success = FileHelper::uploadFile($user, $request->example_upload_multiple_file, "example_upload_multiple_file");
            
            if($success != "success"){
                return response()->json(['title' => 'Gagal', 'status' => 'error', 'detail' => $success],422);
            }
        }
        //End Multiple Upload File with overwrite existing files

        //Multiple Upload File without cleanup/delete existing file
        if($request->example_upload_multiple_file){

            //Upload Files. Refer inside vendor/developer-unijaya/laravel-uploaded-file/FileHelper to understand parameter
            $success = FileHelper::uploadFile($user, $request->example_upload_multiple_file, "example_upload_multiple_file");
            
            if($success != "success"){
                return response()->json(['title' => 'Gagal', 'status' => 'error', 'detail' => $success],422);
            }
        }
        //End Multiple Upload File without cleanup/delete existing file
    }
}

To Retrieve The File From Model (Single) *Using Dropify to Preview

 <div class="col-md-6 col-12">
    <div class="form-group">
        <label class="form-label" for="example_upload_one_file">Document Image (One File) <span style="color:red;">*</span></label>
        <input type="file" class="form-control dropify" name="example_upload_one_file" id="example_upload_one_file"
            data-allowed-file-extensions="png webp jpg jpeg"
            data-default-file="{{ $testForm?->uploadedFiles('example_upload_one_file')->first() ? asset($testForm->uploadedFiles('example_upload_one_file')->first()->path) : ""}}"/>
    </div>
</div>

To Retrieve The File From Model (Multiple)

<div class="table-responsive">
    <table class="table">
        <thead>
            <tr>
                <th width="70%" style="text-align:center">Document</th>
                <th width="30%" style="text-align:center">Action</th>
            </tr>
        </thead>
        <tbody>
            @foreach($user?->uploadedFiles('example_upload_multiple_file')->get() as $key => $value)
            <tr>
                <td>{{$value->original_filename ?? "-"}}</td>
                <td style="text-align:center">
                    <a href="{{ asset($value->path) }}" target="_blank"><i class="fa-solid fa-download"></i></a>
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
</div>

Credits

License

The MIT License (MIT). Please see License File for more information.

Support Spatie Laravel Package Manager

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.