PHP code example of dongttfd / laravel-upload-model

1. Go to this page and download the library: Download dongttfd/laravel-upload-model 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/ */

    

dongttfd / laravel-upload-model example snippets



namespace App\Models;

use DongttFd\LaravelUploadModel\Contracts\UploadOnEloquentModel;
use DongttFd\LaravelUploadModel\Eloquent\UploadFileEloquent;

class User extends Model implements UploadOnEloquentModel
{
    use UploadFileEloquent;

    ...
}

...

// $file must is instance of Illuminate\Http\UploadedFile
$file = $request->file('avatar');

User::create(['avatar' => $file]);


...

// $file must is instance of Illuminate\Http\UploadedFile
$file = $request->file('avatar');

$user->update(['avatar' => $file]);


...

// $file must is instance of Illuminate\Http\UploadedFile
$file = $request->file('avatar');

$user->update(['avatar' => $file]);

// path of file
$user->avatar;

// url of file
$user->avatar_url;


// JSON object

$front = $request->file('front');
$back = $request->file('back');

$user->create([
    ...
    'card' => [
        'front' => $front,
        'back' => $back
    ]
]);

// retries path of file
$user->card['front'];
$user->card['back'];

// retries url of file
$user->card['front_url'];
$user->card['back_url'];


// JSON Array
$photos = $request->file('photos'); // [UploadedFile, UploadedFile]

$post->create([
    ...
    'photos' => $photos
]);

// retries path of file
$post->photos; // ['<photo1-path>', <photo2-path>]

// retries url of file
$post->photos_url; // ['<photo1-url>', <photo2-url>]


// JSON Array and Object combined
$variant = $request->only([
    'photos',  // UploadedFile[]
    'name', // String
    'key' // String
]);

$post->update([
    ...
    'variant' => $variant
]);

// retries path of file
$post->variant['photos']; // ['<photo1-path>', <photo2-path>]

// retries url of file
$post->variant['photos_url']; // ['<photo1-url>', <photo2-url>]





namespace App\Models;

use DongttFd\LaravelUploadModel\Contracts\UploadOnEloquentModel;
use DongttFd\LaravelUploadModel\Eloquent\UploadFileEloquent;
use Illuminate\Database\Eloquent\Model;

class BaseFileModel extends Model implements UploadOnEloquentModel
{
    use UploadFileEloquent;
}





namespace App\Models;

use DongttFd\LaravelUploadModel\Eloquent\FileModel;

class User extends FileModel
{
}

/**
 * Default save on disk (from keys of app/config/filesystem.php > disks)
 *
 * @var string
 */
protected $saveOnDisk = null;

/**
 * Save file to avatar columns
 *
 * @var array
 */
protected $fileFields = ['avatar'];

/**
 * The attributes that should be cast.
 *
 * @var array
 */
protected $casts = [
    'card' => 'array',
];

/**
 * Save files to photos columns
 *
 * @var array
 */
protected $fileFields = [
    'card.front',
    'card.back'
];

/**
 * The attributes that should be cast.
 *
 * @var array
 */
protected $casts = [
    'photos' => 'array',
];

/**
 * Save files to photos columns
 *
 * @var array
 */
protected $fileFields = ['photos.*'];

/**
 * The attributes that should be cast.
 *
 * @var array
 */
protected $casts = [
    'variant' => 'array',
];

/**
 * Save files to photos columns
 *
 * @var array
 */
protected $fileFields = ['variant.photos.*'];

/**
 * Save path file to folder, format: ['<file-field>' => 'folder-name']
 *
 * @var array
 */
protected $fileFolders = [
    'avatar' => 'avatar',
    'card.front' => 'card-front',
    'variant.photos.*' => 'variant-photos',
];

/**
 * Only s3 amazon: save with publish file
 *
 * @var bool
 */
protected $filePublish = false;

/**
 * Only s3 amazon: expire time (minutes) off private file
 *
 * @var int
 */
protected $fileExpireIn = 5;