| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace Longyi\Blog\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Longyi\Blog\Contracts\Blog as BlogContract;
- use Webkul\Core\Models\ChannelProxy;
- use Illuminate\Support\Facades\Storage;
- use Longyi\Blog\Models\Category;
- class Blog extends Model implements BlogContract
- {
- use HasFactory;
- protected $table = 'blogs';
- protected $fillable = [
- 'name',
- 'slug',
- 'short_description',
- 'description',
- 'channels',
- 'default_category',
- 'author',
- 'author_id',
- 'categorys',
- 'tags',
- 'src',
- 'm_src',
- 'status',
- 'locale',
- 'allow_comments',
- 'meta_title',
- 'meta_description',
- 'meta_keywords',
- 'published_at'
- ];
- /**
- * Appends.
- *
- * @var array
- */
- protected $appends = ['src_url', 'm_src_url', 'assign_categorys'];
- /**
- * @return \Illuminate\Database\Eloquent\Relations\HasOne
- */
- public function category()
- {
- return $this->belongsTo(Category::class, 'default_category');
- }
- /**
- * Get the channels.
- */
- public function channels()
- {
- return $this->belongsToMany(ChannelProxy::modelClass(), 'channels');
- }
- /**
- * Get image url for the category image.
- *
- * @return string
- */
- public function getSrcUrlAttribute()
- {
- if (! $this->src) {
- return;
- }
- return Storage::disk('blog_cdn')->url($this->src);
- }
- /**
- * Get image url for the m_src (mobile) image.
- *
- * @return string
- */
- public function getMSrcUrlAttribute()
- {
- if (! $this->m_src) {
- return;
- }
- return Storage::disk('blog_cdn')->url($this->m_src);
- }
- public function getAssignCategorysAttribute()
- {
- $categorys = array();
- $categories_ids = array_values( array_unique( array_merge( explode( ',', $this->default_category ), explode( ',', $this->categorys ) ) ) );
- if ( is_array($categories_ids) && !empty($categories_ids) && count($categories_ids) > 0 ) {
- $categories = Category::whereIn('id', $categories_ids)->get();
- $categorys = ( !empty($categories) && count($categories) > 0 ) ? $categories : array();
- }
- return $categorys;
- }
- }
|