PHP code example of djurovicigoor / lara-files

1. Go to this page and download the library: Download djurovicigoor/lara-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/ */

    

djurovicigoor / lara-files example snippets


return [
    App\Providers\AppServiceProvider::class,
    ...
    DjurovicIgoor\LaraFiles\LaraFilesProvider::class,
];



return [
    /*
    |--------------------------------------------------------------------------
    | Default disk
    |--------------------------------------------------------------------------
    |
    | The disk on which to store added files by default. Choose
    | one of the disks you've configured in config/filesystems.php.
    */
    'default_disk' => env('LARA_FILE_DISK', 'public'),

    /*
    |--------------------------------------------------------------------------
    | Default visibility
    |--------------------------------------------------------------------------
    |
    | public  => Files are accessible through browser
    | private => Files are not accessible through browser
    */
    'visibility' => env('LARA_FILE_VISIBILITY', 'public'),

    /*
    |--------------------------------------------------------------------------
    | Type of files - relations
    |--------------------------------------------------------------------------
    */
    'types' => [
        'file', 'avatar', 'thumbnail',
    ],
];


/*
|--------------------------------------------------------------------------
| Type of files - relations
|--------------------------------------------------------------------------
*/
'types' => [
    'file', 'avatar', 'thumbnail',
],

'disks' => [
	'local' => [
		'driver' => 'local',
		'root'   => storage_path('app/private'),
		'serve'  => TRUE,
		'throw'  => FALSE,
		'report' => FALSE,
	],
	'public' => [
		'driver'     => 'local',
		'root'       => storage_path('app/public'),
		'url'        => env('APP_URL') . '/storage',
		'visibility' => 'public',
		'throw'      => FALSE,
		'report'     => FALSE,
	],
     's3' => [
        'driver' => 's3',
        'key'       => env('AWS_ACCESS_KEY_ID' , 'Your aws acces key goes here'),
        'secret'    => env('AWS_SECRET_ACCESS_KEY' , 'Your aws secret key goes here'),
        'region'    => env('AWS_DEFAULT_REGION' , 'Your aws default region goes here'),
        'bucket'    => env('AWS_BUCKET' , 'Your aws bucket name goes here'),
        'url'       => env('AWS_URL' , 'https://s3.{REGION}.amazonaws.com/{BUCKET}/'),
    ],
    'DOSpaces' => [
        'driver'   => 's3',
        'key'      => env('DO_SPACES_KEY' , 'Your spaces key goes here'),
        'secret'   => env('DO_SPACES_SECRET' , 'Your spaces secret goes here'),
        'region'   => env('DO_SPACES_REGION' , 'Your spaces region goes here'),
        'bucket'   => env('DO_SPACES_BUCKET' , 'Your spaces bucket goes here'),
        'url'      => env('AWS_URL' , 'https://{BUCKET}.{REGION}.digitaloceanspaces.com/'),
    ]
],

use DjurovicIgoor\LaraFiles\Traits\LaraFileTrait;
use Illuminate\Database\Eloquent\Model;

class Post extends Model {
    
    use LaraFileTrait;
    // ...
}

$post = Post::find($id);
$uploadedFile = $post->uploadHttpFile($request->file('image'), 'avatar')

$post = Post::find($id);
$fileUploadObject = $post->addHttpFile($request->file('image'), 'avatar')

$fileUploadObject->setDisk('s3')

$fileUploadObject->setVisibility('private')

$fileUploadObject->setName('custom-file-name')

$fileUploadObject->setProperties([
    'description' => 'Some description',
    'author' => 'John Doe',
]);

$fileUploadObject->upload();

$post = Post::find($id);
$uploadedFilesCollection = $post->uploadHttpFiles($request->file('images'), 'avatar')

$post = Post::find($id);
$uploadedFile = $post->uploadBase64File($request->get('image'), 'avatar')

$post = Post::find($id);
$fileUploadObject = $post->addBase64File($request->get('image'), 'avatar')

$fileUploadObject->setDisk('s3')

$fileUploadObject->setVisibility('private')

$fileUploadObject->setName('custom-file-name')

$fileUploadObject->setProperties([
    'description' => 'Some description',
    'author' => 'John Doe',
]);

$fileUploadObject->upload();

$post = Post::find($id);
$uploadedFilesCollection = $post->uploadBase64Files($request->file('images'), 'avatar')

$tempLaraFile = LaraFileUploader::uploadForOptimizationAndManipulation(
    uploadedFile: $request->file('file'),
    fileUploaderType: 'http_file',
    type: 'image',
    disk: 'local',
    visibility: 'private',
    name: 'Some image image',
    customProperties: ['description' => 'Lorem ipsum']
);

LaraFile::setNewOrder(['d317a690-cc51-4cf4-a3ca-a11c1e7a8673','c7c2c5a0-9e0c-459f-baf2-b9fa02202acd']);

$laraFile->order = 10;
$laraFile->save();

$post = Post::find($id);
$post->addHttpFile($request->file('image'), 'avatar')
     ->setProperties([
            'description' => 'Some description',
            'author' => 'John Doe',
        ])
     ->upload();

$laraFile->hasCustomProperty('author'); // returns true
$laraFile->getCustomProperty('author'); // returns 'John Doe'

$laraFile->hasCustomProperty('does not exist'); // returns false
$laraFile->getCustomProperty('does not exist'); // returns null

$laraFile->setCustomProperty('name', 'value'); // adds a new custom property
$laraFile->forgetCustomProperty('name'); // removes a custom property

$laraFile = LaraFile::find($id);
   
$laraFile->setCustomProperty('name', 'value'); // adds a new custom property or updates an existing one
$laraFile->forgetCustomProperty('name'); // removes a custom property

$laraFile->save();

$laraFile->getCustomProperty('is_public', false);

$laraFile->hasCustomProperty('group.primaryColor'); // returns true
$laraFile->getCustomProperty('group.primaryColor'); // returns 'red'

$laraFile->hasCustomProperty('nested.does-not-exist'); // returns false
$laraFile->getCustomProperty('nested.does-not-exist'); // returns null


$avatar = User::find($id)->avatar;

$avatar->url;

$avatar->full_path;

$avatar->size;

$avatar->mime_type;

$avatar->last_modified;

$avatar->getContents();

$avatar->download();

$avatar->changeDisk('s3');

$avatar->changeVisibility('public');

$avatar->getTemporaryUrl(now()->addMinutes(30));
bash
php artisan vendor:publish --provider="DjurovicIgoor\LaraFiles\LaraFileServiceProvider"
 bash
php artisan migrate