Blog.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Longyi\Blog\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Longyi\Blog\Contracts\Blog as BlogContract;
  6. use Webkul\Core\Models\ChannelProxy;
  7. use Illuminate\Support\Facades\Storage;
  8. use Longyi\Blog\Models\Category;
  9. class Blog extends Model implements BlogContract
  10. {
  11. use HasFactory;
  12. protected $table = 'blogs';
  13. protected $fillable = [
  14. 'name',
  15. 'slug',
  16. 'short_description',
  17. 'description',
  18. 'channels',
  19. 'default_category',
  20. 'author',
  21. 'author_id',
  22. 'categorys',
  23. 'tags',
  24. 'src',
  25. 'm_src',
  26. 'status',
  27. 'locale',
  28. 'allow_comments',
  29. 'meta_title',
  30. 'meta_description',
  31. 'meta_keywords',
  32. 'published_at'
  33. ];
  34. /**
  35. * Appends.
  36. *
  37. * @var array
  38. */
  39. protected $appends = ['src_url', 'm_src_url', 'assign_categorys'];
  40. /**
  41. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  42. */
  43. public function category()
  44. {
  45. return $this->belongsTo(Category::class, 'default_category');
  46. }
  47. /**
  48. * Get the channels.
  49. */
  50. public function channels()
  51. {
  52. return $this->belongsToMany(ChannelProxy::modelClass(), 'channels');
  53. }
  54. /**
  55. * Get image url for the category image.
  56. *
  57. * @return string
  58. */
  59. public function getSrcUrlAttribute()
  60. {
  61. if (! $this->src) {
  62. return;
  63. }
  64. return Storage::disk('blog_cdn')->url($this->src);
  65. }
  66. /**
  67. * Get image url for the m_src (mobile) image.
  68. *
  69. * @return string
  70. */
  71. public function getMSrcUrlAttribute()
  72. {
  73. if (! $this->m_src) {
  74. return;
  75. }
  76. return Storage::disk('blog_cdn')->url($this->m_src);
  77. }
  78. public function getAssignCategorysAttribute()
  79. {
  80. $categorys = array();
  81. $categories_ids = array_values( array_unique( array_merge( explode( ',', $this->default_category ), explode( ',', $this->categorys ) ) ) );
  82. if ( is_array($categories_ids) && !empty($categories_ids) && count($categories_ids) > 0 ) {
  83. $categories = Category::whereIn('id', $categories_ids)->get();
  84. $categorys = ( !empty($categories) && count($categories) > 0 ) ? $categories : array();
  85. }
  86. return $categorys;
  87. }
  88. }