Add thumbs views in datatables with traits
This commit is contained in:
11
app/Traits/Model/CanComment.php
Normal file
11
app/Traits/Model/CanComment.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits\Model;
|
||||
|
||||
trait CanComment
|
||||
{
|
||||
public function needsCommentApproval($model): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
36
app/Traits/Model/HasComments.php
Normal file
36
app/Traits/Model/HasComments.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
34
app/Traits/Model/Imageable.php
Normal file
34
app/Traits/Model/Imageable.php
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user