This would make more sense as a Trait rather than a Class as you can then get access to the model through $this
If you add all your existing code to a new Trait e.g. App\Traits\Uploadable.php
<?php
namespace App\Traits;
trait Uploadable
{
public function retrieveFileUrl()
{
return ($this->file) ? Storage::disk('s3')->temporaryUrl($this->file, now()->addMinutes(5)) : '';
}
public function uploadToS3($newFile, $path)
{
$this->deleteFromS3();
$this->file = Storage::disk('s3')->putFile($path, $newFile);
$this->save();
}
private function deleteFromS3()
{
if ($this->file) {
Storage::disk('s3')->delete($this->file);
}
}
}
You can the use this is you classes that need it
use App\Traits\Uploadable;
class
{
use Uploadable;
$this->uploadToS3(...)
}
If you need to customise any specific you could add a default getter function in the trait and if you need to override you can do so in the class using the trait or look at adding a getter that returns a property e.g.
// On the trait
public function getUploadedAttribute() {
return $this->uploadedAttribute;
}
// On your model
protected $uploadedAttribute = 'file';