meetup/app/Models/Event.php

96 lines
2.6 KiB
PHP

<?php
namespace App\Models;
use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\HasTranslations;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Event extends Model
{
use CrudTrait;
use HasFactory;
use HasTranslations;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
protected $table = 'events';
// protected $primaryKey = 'id';
// public $timestamps = false;
protected $guarded = ['id'];
protected $fillable = [
'name',
'description',
'applications_start_date',
'application_end_date',
'event_date',
'duration',
'is_active',
'image'
];
// protected $hidden = [];
// protected $dates = [];
protected $translatable = [
'name',
'description',
'duration'
];
/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/
public function events(){
return $this->belongsToMany(Attender::class, 'event_attenders', 'event_id', 'attender_id');
}
/*
|--------------------------------------------------------------------------
| SCOPES
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| ACCESSORS
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| MUTATORS
|--------------------------------------------------------------------------
*/
public function setImageAttribute($value)
{
$attribute_name = "image";
$disk = "public";
$destination_path = "events/images";
$this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path, $fileName = null);
}
public static function boot()
{
parent::boot();
static::deleting(function($obj) {
if($obj->image){
\Storage::disk('public')->delete($obj->image);
}
});
}
}