PHP code example of madebyclowd / laravel-documentable
1. Go to this page and download the library: Download madebyclowd/laravel-documentable 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/ */
madebyclowd / laravel-documentable example snippets
use MadeByClowd\Documentable\Traits\Documentable;
class Invoice extends Model
{
use Documentable;
}
'types' => [
'invoice-pdf' => [
'name' => 'Invoice PDF',
'max_size_mb' => 10,
'allowed_mimes' => ['application/pdf'],
'disk' => 's3', // whatever your bucket disk is called in config/filesystems.php
'path_prefix' => 'invoices',
'
$service = app(\MadeByClowd\Documentable\Services\DocumentService::class);
$type = \MadeByClowd\Documentable\Models\DocumentType::where('code', 'invoice-pdf')->firstOrFail();
$document = $service->upload($request->file('file'), $type, $invoice);
$service->getUrl($document, now()->addMinutes(5));
// Direct PUT:
$presigned = $service->createPresignedUpload($type, $filename);
// ...client PUTs to $presigned['url']...
$document = $service->finalizeDirectUpload($presigned['path'], $type, $invoice, $filename, $expectedHash);
// Multipart:
$session = $service->initiateMultipartUpload($filename, $type, $userId);
$url = $service->generatePartUploadUrl($session['path'], $session['upload_id'], $userId, $partNumber, $type);
// ...client PUTs to $url for each part...
$document = $service->completeMultipartUpload(
$session['path'], $session['upload_id'], $userId, $type, $invoice, $filename,
clientParts: null, expectedHash: $hash
);
// Start a new independent slot (e.g. "attachment #2"):
$attachment2 = $service->upload($file, $type, $invoice);
// Add a new version *to that specific slot*:
$service->upload($newFile, $type, $invoice, documentGroupId: $attachment2->document_group_id);
$document = $service->uploadDetached($file, $type, pending: true, ttlHours: 24);
// ...
$service->reassociateDocument($document, $invoice);
$document->commit();
class TenantScopedDedupScope implements \MadeByClowd\Documentable\Contracts\ResolvesDedupScope
{
public function scopeKey(string $hash, ?Model $documentable): string
{
return ($documentable?->tenant_id ?? 'none').':'.$hash;
}
}
// config/documentable.php
'dedup' => ['scope_resolver' => TenantScopedDedupScope::class],
Event::listen(function (\MadeByClowd\Documentable\Events\DocumentUploaded $event) {
GenerateThumbnail::dispatch($event->document);
});
'disk' => env('DOCUMENTABLE_DISK', 's3'),
'load_migrations' => true,
'load_routes' => true,
'middleware' => ['api'], // ['web', 'auth'] for a session-based monolith — see "Uploading via the shipped HTTP API"
'types' => [/* code-first DocumentType catalog, keyed by code */],
'multipart' => [
'threshold_bytes' => 10 * 1024 * 1024,
'etag_strategy' => 'server-authoritative', // or 'client'
'part_upload_url_ttl' => '+1 hour',
'session_ttl_hours' => 24,
'use_native_checksum' => false, // optional S3 additional-checksums fast path
'drivers' => ['s3' => S3MultipartDriver::class],
],
'lifecycle' => ['pending_ttl_hours' => 24, 'reaper_frequency' => 'hourly'],
'listing' => ['per_page' => 50], // GET /documents page size
'authorization' => ['resolver' => null], // bind AuthorizesDocumentAccess
'dedup' => ['scope_resolver' => null], // bind ResolvesDedupScope
'security' => [
'scanner' => null, // bind ScansUploadedFile
'allowed_documentable_types' => null, // see "Security" section below
],
'storage_path' => ['generator' => null], // bind GeneratesStoragePath
'disks' => [/* per-disk server_side_encryption / kms_key_id */],
'throttle' => 'documents', // named rate limiter for the shipped routes
'audit' => ['enabled' => false, 'access_log' => false],
// AppServiceProvider::boot()
Relation::enforceMorphMap([
'user' => \App\Models\User::class,
]);
bash
php artisan documents:install
bash
php artisan documents:attach-model Invoice
bash
php artisan documents:sync-types
bash
php artisan vendor:publish --tag=documentable-config
php artisan vendor:publish --tag=documentable-migrations
php artisan migrate