Attendize/app/Models/HelpTicket.php

99 lines
3.0 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
class HelpTicket extends Model
{
use CrudTrait;
use Notifiable;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
protected $table = 'help_tickets';
// protected $primaryKey = 'id';
// public $timestamps = false;
// protected $guarded = ['id'];
protected $fillable = ['code','email','name','phone','text','subject', 'attachment','status','ticket_category_id'];
// protected $hidden = [];
// protected $dates = [];
/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/
public function topic(){
return $this->belongsTo(HelpTopic::class,'ticket_category_id');
}
public function comments(){
return $this->hasMany(HelpTicketComment::class);
}
/*
|--------------------------------------------------------------------------
| SCOPES
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| ACCESORS
|--------------------------------------------------------------------------
*/
public function getOwnerAttribute(){
return $this->name ?? $this->email;
}
/*
|--------------------------------------------------------------------------
| MUTATORS
|--------------------------------------------------------------------------
*/
public function setAttachmentAttribute($file){
$attribute_name = "attachment";
$disk = config('filesystems.default'); // or use your own disk, defined in config/filesystems.php
$destination_path = "help"; // path relative to the disk above
if($file){
$filename = md5($file.time()) . '.' . strtolower($file->getClientOriginalExtension());
// 2. Move the new file to the correct path
$file_path = $file->storeAs($destination_path, $filename, $disk);
$this->attributes[$attribute_name] = $file_path;
}
}
/**
* Boot all of the bootable traits on the model.
*/
public static function boot()
{
parent::boot();
static::creating(function ($ticket) {
$ticket->code = strtoupper(str_random(5)) . date('jn');
});
static::deleting(function($obj) {
$disk = config('filesystems.default');
\Storage::disk($disk)->delete($obj->attachment);
});
}
}