1. Go to this page and download the library: Download nadlambino/uploadable 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/ */
nadlambino / uploadable example snippets
return [
/*
|--------------------------------------------------------------------------
| Validation
|--------------------------------------------------------------------------
|
| Enable or disable the package's validation rules. Set to false if validation
| has been performed separately, such as in a form request.
|
*/
'validate' => true,
/*
|--------------------------------------------------------------------------
| Uploads Model
|--------------------------------------------------------------------------
|
| Specify the model to use for uploads.
|
*/
'uploads_model' => \NadLambino\Uploadable\Models\Upload::class,
/*
|--------------------------------------------------------------------------
| Delete Model on Upload Failure
|--------------------------------------------------------------------------
|
| Automatically delete the newly created model if the upload process fails.
| Applicable only to models that are being created.
|
*/
'delete_model_on_upload_fail' => true,
/*
|--------------------------------------------------------------------------
| Rollback Model on Upload Failure
|--------------------------------------------------------------------------
|
| Revert changes made to an existing model if the upload fails, restoring
| the model's original attributes. Applies only to updated models.
|
*/
'rollback_model_on_upload_fail' => true,
/*
|--------------------------------------------------------------------------
| Force Delete Uploads
|--------------------------------------------------------------------------
|
| Determines whether uploaded files are permanently deleted. By default,
| files are soft deleted, allowing for recovery.
|
*/
'force_delete_uploads' => false,
/*
|--------------------------------------------------------------------------
| Replace Previous Uploads
|--------------------------------------------------------------------------
|
| Determines whether uploaded files should be replaced with new ones. If
| false, new files will be uploaded. If true, previous files will be
| deleted once the new ones are uploaded.
|
*/
'replace_previous_uploads' => false,
/*
|--------------------------------------------------------------------------
| Upload Queue
|--------------------------------------------------------------------------
|
| Specify the queue name for uploading files. If set to null, uploads are
| processed immediately. Otherwise, files are queued and processed.
|
*/
'upload_on_queue' => null,
/*
|--------------------------------------------------------------------------
| Delete Model on Queued Upload Failure
|--------------------------------------------------------------------------
|
| Delete the newly created model if a queued upload fails. Only affects models
| that are being created.
|
*/
'delete_model_on_queue_upload_fail' => false,
/*
|--------------------------------------------------------------------------
| Rollback Model on Queued Upload Failure
|--------------------------------------------------------------------------
|
| Revert changes to a model if a queued upload fails, using the model's original
| attributes before the upload started. Affects only updated models.
|
*/
'rollback_model_on_queue_upload_fail' => false,
/*
|--------------------------------------------------------------------------
| Temporary Storage Disk
|--------------------------------------------------------------------------
|
| Define the disk for temporary file storage during queued uploads. This
| is where files are stored before being processed.
|
*/
'temporary_disk' => 'local',
/*
|--------------------------------------------------------------------------
| Temporary URL
|--------------------------------------------------------------------------
|
| Temporary URL for files that are uploaded locally is not supported by the
| local disk. This setting allows you to specify the path and middleware to
| access the files temporarily which uses a signed URL under the hood.
| `expiration` can be a string or an instance of `DateTimeInterface`.
|
*/
'temporary_url' => [
'path' => '/temporary',
'middleware' => ['signed'],
'expiration' => '1 hour',
],
/*
|--------------------------------------------------------------------------
| Allowed Mimes by Extension
|--------------------------------------------------------------------------
|
| Specify the mime types by extension that is allowed for uploads. Supports
| categorization for images, videos, and documents with specific file
| extensions.
|
*/
'mimes' => [
'image' => ['jpeg', 'jpg', 'png', 'gif', 'bmp', 'svg', 'webp', 'ico'],
'video' => ['mp4', 'webm', 'avi', 'mov', 'wmv', 'flv', '3gp', 'mkv', 'mpg', 'mpeg'],
'document' => ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'csv', 'txt'],
],
];
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use NadLambino\Uploadable\Concerns\Uploadable;
class Post extends Model
{
use Uploadable;
}
protected function uploadRules(): array
{
return [
// Override the rules for `document` field
'document' => ['
public function uploadRuleMessages(): array
{
return [
'document.equired' => 'The avatar is
public function getUploadFilename(UploadedFile $file): string
{
return str_replace('.', '', microtime(true)).'-'.$file->hashName();
}
public function getUploadPath(UploadedFile $file): string
{
return $this->getTable().DIRECTORY_SEPARATOR.$this->{$this->getKeyName()};
}
public function getUploadStorageOptions(): array
{
return [
'visibility' => 'public',
'CacheControl' => 'max-age=315360000, no-transform, public'
];
}
// Let us say that on your config/filesystems.php, your default disk is set to `local`.
// This method will create a user and will upload the file from the request, e.g., user avatar.
public function store(Request $request)
{
User::create(...);
}
// While this method store the message and will upload the file from the request to `s3`.
public function store(Request $request)
{
Message::uploadDisk('s3');
Message::create(...);
}
public function store(Request $request)
{
Post::uploadToCollection('banner');
Post::create(...);
}
public function update(Request $request, Post $post)
{
$post->update($request->all());
// If the post did not change, the `updated` event won't be fired.
// So, we need to manually call the `updateUploads` method.
if (! $post->wasChanged()) {
$post->updateUploads();
}
}
public function update(Request $request, Post $post)
{
// Temporarily disable the file uploads
Post::disableUpload();
$post->update($request->all());
// Do more stuff here...
// Manually process the uploads after everything you want to do
$post->updateUploads();
}
use NadLambino\Uploadable\Actions\Upload;
public function store(Request $request)
{
// Disable the uploads for all of the instances of Post model
Upload::disableFor(Post::class);
// Files will be uploaded for User model
User::create(...);
// Files won't be uploaded for Post model
Post::create(...);
}
// OR
public function update(Request $request, User $user)
{
// Disable the uploads only for this specific $user
Upload::disableFor($user);
// Files won't be uploaded for this specific $user
$user->update($request->validated());
$anotherUser = User::find(...);
// Files will be uploaded for this $anotherUser
$anotherUser->update(...);
}
use NadLambino\Uploadable\Actions\Upload;
public function store()
{
// Process the uploads only for all of the instances of User model
Upload::onlyFor(User::class);
// Files will be uploaded for this User model
User::create(...);
// Files won't be uploaded for this Post model
Post::create(...);
}
public function update(User $user)
{
// Process the uploads only for this specific $user
Upload::onlyFor($user);
// Files will be uploaded for this specific $user
$user->update(...);
$anotherUser = User::find(...);
// Files won't be uploaded for this $anotherUser
$anotherUser->update(...)
}
'replace_previous_uploads' => true,
public function update(Request $request, Post $post)
{
// Replace the previous uploads
Post::replacePreviousUploads();
$post->update($request->all());
}
// DO
$post->uploadFrom($request->file('image'));
// OR
$post->uploadFrom(new UploadedFile(...));
// OR
$post->uploadFrom([
$request->file('image'),
$request->file('avatar')
]);
// OR
$fullpath = ... // The path of the file that is uploaded in your `temporary_disk`. This could be something like an image that was modified by `ImageIntervention` then temporarily stored before uploading
$post->uploadFrom($fullpath);
// OR
$post->uploadFrom([
$fullpath1,
$fullpath2
]);
// OR even a mixed of both
$post->uploadFrom([
$request->file('image'),
$fullpath,
]);
$post->save();
// Relation for all types of uploads
public function upload(): MorphOne { }
// Relation for all types of uploads
public function uploads(): MorphMany { }
// Relation for uploads where extension or type is in the accepted image mimes
public function image(): MorphOne { }
// Relation for uploads where extension or type is in the accepted image mimes
public function images(): MorphMany { }
// Relation for uploads where extension or type is in the accepted video mimes
public function video(): MorphOne { }
// Relation for uploads where extension or type is in the accepted video mimes
public function videos(): MorphMany { }
// Relation for uploads where extension or type is in the accepted document mimes
public function document(): MorphOne { }
// Relation for uploads where extension or type is in the accepted document mimes
public function documents(): MorphMany { }
public function beforeSavingUpload(Upload $upload, Model $model) : void
{
$upload->additional_field = "some value";
}
Post::beforeSavingUploadUsing(function (Upload $upload, Post $model) use ($value) {
$model->additional_field = $value;
});
$post->save();