Files
new_raffles/Modules/Document/app/Models/Document.php
Subash efa9231391 feat: Implement Document Module with Dropzone file upload functionality
- Added DocumentController for handling document uploads and management.
- Created Document model with necessary attributes and relationships.
- Implemented DocumentService for business logic related to documents.
- Set up routes for document management in both web and API contexts.
- Developed views for document upload using Dropzone for file handling.
- Included necessary assets and styles for the Document module.
- Created migration for documents table with appropriate fields.
- Added configuration and service provider for the Document module.
2025-08-01 18:00:15 +05:45

70 lines
1.8 KiB
PHP

<?php
namespace Modules\Document\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class Document extends Model
{
use HasFactory;
protected $fillable = ['title', 'file_path'];
public function documentable(): MorphTo
{
return $this->morphTo();
}
public function getUrl()
{
$path = $this->document_path;
return Storage::disk('public')->url($path);
}
public function getSize()
{
$path = $this->document_path;
if (Storage::disk('public')->exists($path)) {
$sizeInBytes = Storage::disk('public')->size($path);
return round($sizeInBytes / 1024, 2) . " KB";
}
return 0;
}
public function getExtension()
{
return pathinfo($this->document_path, PATHINFO_EXTENSION);
}
public function isImageFile()
{
$imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp', 'tiff', 'tif', 'ico'];
$extension = pathinfo($this->document_path, PATHINFO_EXTENSION);
return in_array(Str::lower($extension), $imageExtensions);
}
protected function documentPath(): Attribute
{
return Attribute::make(
get: function (mixed $value, array $attributes) {
$collectionName = $attributes['collection_name'];
$path = $attributes['document_path'];
return "{$collectionName}/{$path}";
}
);
}
public function scopeActive($query, int $status = 1)
{
return $query->where('status', $status);
}
}