Add thumbs views in datatables with traits

This commit is contained in:
Ludovic CANDELLIER
2021-11-01 18:37:25 +01:00
parent ae20643879
commit 900da34b57
17 changed files with 127 additions and 106 deletions

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Traits\Model;
trait CanComment
{
public function needsCommentApproval($model): bool
{
return true;
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Traits\Model;
use Illuminate\Database\Eloquent\Model;
use App\Contracts\Commentator;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use App\Models\Core\Comment;
trait HasComments
{
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
public function comment(string $comment)
{
return $this->commentAsUser(auth()->user(), $comment);
}
public function commentAsUser(?Model $user, string $comment)
{
$comment = new Comment([
'comment' => $comment,
'is_approved' => ($user instanceof Commentator) ? ! $user->needsCommentApproval($this) : false,
'user_id' => is_null($user) ? null : $user->getKey(),
'commentable_id' => $this->getKey(),
'commentable_type' => get_class(),
]);
return $this->comments()->save($comment);
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Traits\Model;
use Spatie\Image\Manipulations;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\InteractsWithMedia;
use App\Repositories\Core\Medias;
trait Imageable
{
use InteractsWithMedia;
public function images()
{
return $this->hasMany('App\Models\Core\Media', 'model_id')->where('model_type', get_class($this));
}
public function image()
{
return $this->hasOne('App\Models\Core\Media', 'model_id')->where('model_type', get_class($this));
}
public function registerMediaConversions(Media $media = null) : void
{
$this->addMediaConversion('thumb')->fit(Manipulations::FIT_CROP, 32, 32);
$this->addMediaConversion('mini')->fit(Manipulations::FIT_CROP, 96, 96);
$this->addMediaConversion('preview')->fit(Manipulations::FIT_CROP, 160, 160);
$this->addMediaConversion('normal')->fit(Manipulations::FIT_CROP, 480, 480);
// $this->addMediaConversion('zoom')->fit(Manipulations::FIT_CROP, 1200, 1200)->withResponsiveImages();
}
}