Information.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Repositories\Models\Inform;
  3. use App\Repositories\Models\Base\Admin;
  4. use App\Repositories\Models\Model;
  5. use App\Repositories\Models\Base\Resource;
  6. use Carbon\Carbon;
  7. use Illuminate\Support\Facades\DB;
  8. use Prettus\Repository\Contracts\Transformable;
  9. use Prettus\Repository\Traits\TransformableTrait;
  10. /**
  11. * Class Information.
  12. *
  13. * @package namespace App\Repositories\Models\Inform;
  14. */
  15. class Information extends Model implements Transformable
  16. {
  17. use TransformableTrait;
  18. protected $table = 'inform_information';
  19. protected static function booted()
  20. {
  21. self::language();
  22. static::saved(function (Information $information) {
  23. if ($information->isDirty('is_message')) {
  24. if ($information->is_message == 0) {
  25. UserMessage::query()->where('information_id', $information->id)->delete();
  26. }
  27. if ($information->is_message == 1) {
  28. $role_ids = Category::query()->where('id', $information->category_id)->value('role_ids');
  29. $user_ids = Admin::query()->where('role_id', 'in', $role_ids)->pluck('id');
  30. $data = [];
  31. $now = Carbon::now();
  32. foreach ($user_ids as $user_id) {
  33. // if ($user_id != $information->admin_id) {
  34. $data[] = [
  35. 'user_id' => $user_id,
  36. 'information_id' => $information->id,
  37. 'category_id' => $information->category_id,
  38. 'published_at' => $information->published_at,
  39. 'admin_id' => $information->admin_id,
  40. 'created_at' => $now,
  41. 'updated_at' => $now,
  42. ];
  43. // }
  44. }
  45. UserMessage::query()->insert($data);
  46. }
  47. }
  48. });
  49. }
  50. public function cover_resource()
  51. {
  52. return $this->belongsTo(Resource::class, 'cover', 'id')->select(['path', 'id', 'url']);
  53. }
  54. /**
  55. * The attributes that are mass assignable.
  56. *
  57. * @var array
  58. */
  59. protected $guarded = [];
  60. public function setTagsAttribute($val)
  61. {
  62. $this->attributes['tags'] = '-' . arr2str($val, '-') . '-';
  63. }
  64. public function getTagsAttribute($val)
  65. {
  66. return str2arr(trim($val, '-'), '-');
  67. }
  68. public function category()
  69. {
  70. return $this->belongsTo(Category::class);
  71. }
  72. public function admin()
  73. {
  74. return $this->belongsTo(Admin::class);
  75. }
  76. }