PHP code example of esign / laravel-model-files

1. Go to this page and download the library: Download esign/laravel-model-files library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

esign / laravel-model-files example snippets


use Esign\ModelFiles\Concerns\HasFiles;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFiles;
}

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->boolean('file')->default(0);
    $table->string('file_filename')->nullable();
    $table->string('file_mime')->nullable();
});

use Esign\ModelFiles\Concerns\HasFiles;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFiles;

    public function getFileDisk(): string
    {
        return 'public';
    }
}

$post->storeFile('file', $request->file('attachment'));

$post->hasFile('file'); // returns true/false
$post->getFolderPath('file'); // returns posts/file
$post->getFilePath('file'); // returns posts/file/1.pdf
$post->getFilePathOnDisk('file'); // returns /path/to/storage/app/public/posts/file/1.pdf
$post->getFileMime('file'); // returns application/pdf
$post->getFileExtension('file'); // returns pdf
$post->getFileUrl('file'); // returns https://www.example.com/storage/posts/file/1.pdf
$post->getVersionedFileUrl('file'); // returns https://www.example.com/storage/posts/file/1.pdf?t=1675776047

$post->deleteFile('file');

use Esign\ModelFiles\Concerns\HasFiles;
use Esign\UnderscoreTranslatable\UnderscoreTranslatable;
use Illuminate\Database\Eloquent\Model;

class UnderscoreTranslatablePost extends Model
{
    use HasFiles;
    use UnderscoreTranslatable;

    public $translatable = [
        'document',
        'document_filename',
        'document_mime',
    ];
}

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->boolean('document_en')->default(0);
    $table->boolean('document_nl')->default(0);
    $table->string('document_filename_en')->nullable();
    $table->string('document_filename_nl')->nullable();
    $table->string('document_mime_en')->nullable();
    $table->string('document_mime_nl')->nullable();
});

$post->hasFile('document'); // returns true/false
$post->getFolderPath('document'); // returns posts/document_en
$post->getFilePath('document'); // returns posts/document_en/1.pdf
$post->getFileMime('document'); // returns application/pdf
$post->getFileExtension('document'); // returns pdf
$post->getFileUrl('document'); // returns https://www.example.com/storage/posts/document_en/1.pdf
$post->getVersionedFileUrl('document'); // returns https://www.example.com/storage/posts/document_en/1.pdf?t=1675776047

$post->hasFile('document_en'); // returns true/false
$post->getFolderPath('document_en'); // returns posts/document_en
$post->getFilePath('document_en'); // returns posts/document_en/1.pdf
$post->getFileMime('document_en'); // returns application/pdf
$post->getFileExtension('document_en'); // returns pdf
$post->getFileUrl('document_en'); // returns https://www.example.com/storage/posts/document_en/1.pdf
$post->getVersionedFileUrl('document_en'); // returns https://www.example.com/storage/posts/document_en/1.pdf?t=1675776047