journal-intime/app/Post.php

76 lines
1.7 KiB
PHP

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Spatie\Image\Exceptions\InvalidManipulation;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\Models\Media;
class Post extends Model implements HasMedia
{
use HasMediaTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'content', 'image', 'date_post', 'user_id'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'date_post' => 'datetime',
];
/**
* Delete this posts tags
*/
public function deleteTags()
{
$tags_to_delete = PostsTag::where('post_id', $this->id)->get();
foreach ($tags_to_delete as $tag_to_delete) {
$tag_to_delete->delete();
}
}
public static function getUserPostForMonth($user_id, $year, $month)
{
return self::whereMonth('date_post', $month)
->whereYear('date_post', $year)
->where('user_id', $user_id)
->orderBy('date_post')
->get();
}
/**
* @param Media|null $media
*/
public function registerMediaConversions(Media $media = null)
{
try {
$this->addMediaConversion('list')
->width(300)
->optimize();
} catch (InvalidManipulation $e) {
Log::alert(sprintf('Error while manipulating Post Image for %s (%s)', $this->id, $e->getMessage()));
}
}
}