PHP code example of oi-lab / oi-laravel-attachments
1. Go to this page and download the library: Download oi-lab/oi-laravel-attachments 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/ */
oi-lab / oi-laravel-attachments example snippets
return [
// Model used for the created_by / updated_by audit relationships
'user_model' => 'App\Models\User',
// Model classes used by the package — override with your own subclasses
'models' => [
'file' => OiLab\OiLaravelAttachments\Models\File::class,
'folder' => OiLab\OiLaravelAttachments\Models\Folder::class,
'attachment' => OiLab\OiLaravelAttachments\Models\Attachment::class,
],
// Disk used to store uploaded files (defaults to ATTACHMENTS_DISK, then FILESYSTEM_DISK)
'disk' => env('ATTACHMENTS_DISK', env('FILESYSTEM_DISK', 'local')),
// Directory uploaded files are stored under
'directory' => 'uploads',
];
use Illuminate\Database\Eloquent\Model;
use OiLab\OiLaravelAttachments\Concerns\HasAttachments;
class Product extends Model
{
use HasAttachments;
}
// Attach a File (model instance or id) to a collection
$product->attachFile($file, collection: 'gallery');
// Detach
$product->detachFile($file, 'gallery');
// Read attachments (ordered by sort)
$product->attachments; // all attachments
$product->attachments('gallery')->get(); // one collection
$product->singleAttachment('cover'); // MorphOne for single-file collections
// Read the underlying File models directly
$product->attached_files; // Collection<File>
$product->syncAttachments([$id1, $id2, $id3], 'gallery');
// Returns false and does nothing if the collection is already in this exact state
$changed = $product->syncAttachmentsIfChanged([$id1, $id2, $id3], 'gallery');
// Map file ids to their new sort position
$product->reorderAttachments([
$fileA->id => 0,
$fileB->id => 1,
$fileC->id => 2,
], 'gallery');
use OiLab\OiLaravelAttachments\Actions\StoreUploadedFile;
use OiLab\OiLaravelAttachments\Actions\AttachUploadedFiles;
public function store(Request $request, Product $product): RedirectResponse
{
// Store a single upload, get back a File model
$file = StoreUploadedFile::handle($request->file('document'));
// Store multiple uploads and attach them to the product
AttachUploadedFiles::handle($product, $request->file('images'), 'gallery');
return back();
}
$file->isImage(); // mimetype starts with image/
$file->isVideo();
$file->isAudio();
$file->getFullPath(); // absolute path (local disks only)
$file->getStream(); // stream resource (works with S3 etc.)
File::search('invoice')->get(); // matches filename, title, or description