Attendize/app/Models/Slider.php

102 lines
3.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
2023-05-08 13:38:37 +00:00
use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Illuminate\Database\Eloquent\Model;
2020-04-06 12:47:48 +00:00
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
class Slider extends Model
{
use CrudTrait;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
protected $table = 'sliders';
// protected $primaryKey = 'id';
// public $timestamps = false;
// protected $guarded = ['id'];
2020-04-06 13:34:26 +00:00
protected $fillable = ['text_tk','text_ru','image','active','link','title_ru','title_tk','order'];
// protected $hidden = [];
// protected $dates = [];
/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/
public static function boot()
{
parent::boot();
static::deleting(function($obj) {
\Storage::disk('local')->delete($obj->image);
});
}
2020-04-06 12:47:48 +00:00
/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| SCOPES
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| ACCESORS
|--------------------------------------------------------------------------
*/
2020-04-06 12:47:48 +00:00
public function getTitleAttribute(){
2020-04-06 13:02:52 +00:00
return $this->{'title_'.Config::get('app.locale')};
2020-04-06 12:47:48 +00:00
}
public function getTextAttribute(){
2020-04-06 13:02:52 +00:00
return $this->{'text_'.Config::get('app.locale')};
2020-04-06 12:47:48 +00:00
}
public function setImageAttribute($value)
{
$attribute_name = "image";
$disk = 'local'; // or use your own disk, defined in config/filesystems.php
$destination_path = "sliders"; // path relative to the disk above
// if the image was erased
if ($value==null) {
// delete the image from disk
\Storage::disk($disk)->delete($this->{$attribute_name});
// set null in the database column
$this->attributes[$attribute_name] = null;
}
// if a base64 was sent, store it in the db
if (starts_with($value, 'data:image'))
{
// 0. Make the image
$image = \Image::make($value)->encode('jpg', 90);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
// 2. Store the image on disk.
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
// 3. Save the public path to the database
// but first, remove "public/" from the path, since we're pointing to it from the root folder
// that way, what gets saved in the database is the user-accesible URL
$public_destination_path = Str::replaceFirst('public/', '', $destination_path);
$this->attributes[$attribute_name] = asset('user_content/'.$public_destination_path.'/'.$filename);
}
}
/*
|--------------------------------------------------------------------------
| MUTATORS
|--------------------------------------------------------------------------
*/
}