Backend for file uploads attached to any generic model, initially on aircraft and airports #226
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use App\Interfaces\Model;
|
||||
use App\Models\Enums\AircraftStatus;
|
||||
use App\Models\Traits\ExpensableTrait;
|
||||
use App\Models\Traits\FilesTrait;
|
||||
|
||||
/**
|
||||
* @property int id
|
||||
@@ -22,6 +23,7 @@ use App\Models\Traits\ExpensableTrait;
|
||||
class Aircraft extends Model
|
||||
{
|
||||
use ExpensableTrait;
|
||||
use FilesTrait;
|
||||
|
||||
public $table = 'aircraft';
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use App\Interfaces\Model;
|
||||
use App\Models\Traits\ExpensableTrait;
|
||||
use App\Models\Traits\FilesTrait;
|
||||
|
||||
/**
|
||||
* Class Airport
|
||||
@@ -16,6 +17,7 @@ use App\Models\Traits\ExpensableTrait;
|
||||
class Airport extends Model
|
||||
{
|
||||
use ExpensableTrait;
|
||||
use FilesTrait;
|
||||
|
||||
public $table = 'airports';
|
||||
public $timestamps = false;
|
||||
|
||||
53
app/Models/File.php
Normal file
53
app/Models/File.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
|
||||
use App\Interfaces\Model;
|
||||
use App\Models\Traits\ReferenceTrait;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
/**
|
||||
* File property
|
||||
* @property string $name
|
||||
* @property string $description
|
||||
* @property string $path
|
||||
* @property boolean $public
|
||||
* @package App\Models
|
||||
*/
|
||||
class File extends Model
|
||||
{
|
||||
use ReferenceTrait;
|
||||
|
||||
protected $table = 'files';
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'description',
|
||||
'path',
|
||||
'public',
|
||||
'ref_model',
|
||||
'ref_model_id',
|
||||
];
|
||||
|
||||
public static $rules = [
|
||||
'name' => 'required',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the full URL to this attribute
|
||||
* @return string
|
||||
*/
|
||||
public function getUrlAttribute(): string
|
||||
{
|
||||
$disk = config('filesystems.public_files');
|
||||
if ($disk !== 'public') {
|
||||
return Storage::disk(config('filesystems.public_files'))
|
||||
->url($this->path);
|
||||
}
|
||||
|
||||
return public_asset(Storage::disk('public')
|
||||
->url($this->path)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Interfaces\Model;
|
||||
use App\Models\Traits\ReferenceTrait;
|
||||
|
||||
/**
|
||||
* @property string id UUID type
|
||||
@@ -22,6 +23,8 @@ use App\Interfaces\Model;
|
||||
*/
|
||||
class JournalTransaction extends Model
|
||||
{
|
||||
use ReferenceTrait;
|
||||
|
||||
protected $table = 'journal_transactions';
|
||||
|
||||
public $incrementing = false;
|
||||
@@ -61,33 +64,6 @@ class JournalTransaction extends Model
|
||||
return $this->belongsTo(Journal::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Model $object
|
||||
* @return JournalTransaction
|
||||
*/
|
||||
public function referencesObject($object)
|
||||
{
|
||||
$this->ref_model = \get_class($object);
|
||||
$this->ref_model_id = $object->id;
|
||||
$this->save();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function getReferencedObject()
|
||||
{
|
||||
if ($classname = $this->ref_model) {
|
||||
$klass = new $this->ref_model;
|
||||
|
||||
return $klass->find($this->ref_model_id);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $currency
|
||||
*/
|
||||
|
||||
22
app/Models/Traits/FilesTrait.php
Normal file
22
app/Models/Traits/FilesTrait.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Traits;
|
||||
|
||||
use App\Models\File;
|
||||
|
||||
trait FilesTrait
|
||||
{
|
||||
/**
|
||||
* Morph to type of File
|
||||
* @return mixed
|
||||
*/
|
||||
public function files()
|
||||
{
|
||||
return $this->morphMany(
|
||||
File::class,
|
||||
'files', # overridden by the next two anyway
|
||||
'ref_model',
|
||||
'ref_model_id'
|
||||
);
|
||||
}
|
||||
}
|
||||
40
app/Models/Traits/ReferenceTrait.php
Normal file
40
app/Models/Traits/ReferenceTrait.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Traits;
|
||||
|
||||
/**
|
||||
* Trait ReferenceTrait
|
||||
* @property \App\Interfaces\Model $ref_model
|
||||
* @property mixed $ref_model_id
|
||||
* @package App\Models\Traits
|
||||
*/
|
||||
trait ReferenceTrait
|
||||
{
|
||||
/**
|
||||
* @param \App\Interfaces\Model $object
|
||||
* @return self
|
||||
*/
|
||||
public function referencesObject($object)
|
||||
{
|
||||
$this->ref_model = \get_class($object);
|
||||
$this->ref_model_id = $object->id;
|
||||
$this->save();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the object or null
|
||||
* @return \App\Interfaces\Model|null
|
||||
*/
|
||||
public function getReferencedObject()
|
||||
{
|
||||
if ($classname = $this->ref_model) {
|
||||
$klass = new $this->ref_model;
|
||||
|
||||
return $klass->find($this->ref_model_id);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user