62 lines
1.5 KiB
PHP
62 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Str;
|
|
|
|
class ProjectMailing extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'subject',
|
|
'message',
|
|
];
|
|
|
|
protected static function booted()
|
|
{
|
|
static::created(function (ProjectMailing $projectMailing) {
|
|
Mail::to($projectMailing->recipients()->pluck('email')->toArray())
|
|
->send(new \App\Mail\ReportMailer($projectMailing));
|
|
});
|
|
}
|
|
|
|
public function addAttachment($name, UploadedFile $file)
|
|
{
|
|
$prefix = Str::random(10);
|
|
$originalFileName = $file->getClientOriginalName();
|
|
$extension = pathinfo($originalFileName, PATHINFO_EXTENSION);
|
|
$newFileName = $prefix.'_'.pathinfo($originalFileName, PATHINFO_FILENAME).'.'.$extension;
|
|
|
|
$path = Storage::disk('local')->putFileAs(
|
|
$this->project->attachmentPath, $file, $newFileName
|
|
);
|
|
|
|
$this->attachments()->save(new ProjectMailingAttachment([
|
|
'name' => $name,
|
|
'path' => $path
|
|
]));
|
|
}
|
|
|
|
|
|
public function project()
|
|
{
|
|
return $this->belongsTo(Project::class);
|
|
}
|
|
|
|
public function recipients()
|
|
{
|
|
return $this->hasMany(ProjectMailingRecipient::class);
|
|
}
|
|
|
|
public function attachments()
|
|
{
|
|
return $this->hasMany(ProjectMailingAttachment::class);
|
|
}
|
|
}
|