PHP code example of gearbox-solutions / has-one-file

1. Go to this page and download the library: Download gearbox-solutions/has-one-file 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/ */

    

gearbox-solutions / has-one-file example snippets


// store a file related to a model
$document->storeFile($file);
// store a file related to a model without immediately saving the model
$document->storeFile($file, false);

// delete a previously stored file from a model
$document->deleteFile();
// delete a previously stored file from a model without immediately saving the model
$document->deleteFile(false);

// check if a file exists for a model
$document->fileExists();

// get the URL for a file from a model
// this can be nice to append for a link to the file
$document->file_url;

// get the contents of a file from a model
$document->getFile();

// get the storage path of a file from a model
$document->getFilePath();

// get the storage directory of a file from a model
$document->getStorageDirectory();

// get the storage disk of a file from a model
$document->getFileStorageDisk();


use GearboxSolutions\HasOneFile\Traits\HasOneFile;
...
...

class Document extends Model
{
    use HasOneFile;


Schema::table('documents', function (Blueprint $table) {
    $table->string('file_name')->nullable();
});

class Document extends Model
{
    use HasOneFile;

    protected $fileStorageDisk = 's3';
}

class Document extends Model
{
    use HasOneFile;

    protected $fileNameField = 'document_name';
}