1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace App\Repositories\Models\Inform;
- use App\Repositories\Models\Base\Admin;
- use App\Repositories\Models\Model;
- use App\Repositories\Models\Base\Resource;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- use Prettus\Repository\Contracts\Transformable;
- use Prettus\Repository\Traits\TransformableTrait;
- /**
- * Class Information.
- *
- * @package namespace App\Repositories\Models\Inform;
- */
- class Information extends Model implements Transformable
- {
- use TransformableTrait;
- protected $table = 'inform_information';
- protected static function booted()
- {
- self::language();
- static::saved(function (Information $information) {
- if ($information->isDirty('is_message')) {
- if ($information->is_message == 0) {
- UserMessage::query()->where('information_id', $information->id)->delete();
- }
- if ($information->is_message == 1) {
- $role_ids = Category::query()->where('id', $information->category_id)->value('role_ids');
- $user_ids = Admin::query()->where('role_id', 'in', $role_ids)->pluck('id');
- $data = [];
- $now = Carbon::now();
- foreach ($user_ids as $user_id) {
- // if ($user_id != $information->admin_id) {
- $data[] = [
- 'user_id' => $user_id,
- 'information_id' => $information->id,
- 'category_id' => $information->category_id,
- 'published_at' => $information->published_at,
- 'admin_id' => $information->admin_id,
- 'created_at' => $now,
- 'updated_at' => $now,
- ];
- // }
- }
- UserMessage::query()->insert($data);
- }
- }
- });
- }
- public function cover_resource()
- {
- return $this->belongsTo(Resource::class, 'cover', 'id')->select(['path', 'id', 'url']);
- }
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $guarded = [];
- public function setTagsAttribute($val)
- {
- $this->attributes['tags'] = '-' . arr2str($val, '-') . '-';
- }
- public function getTagsAttribute($val)
- {
- return str2arr(trim($val, '-'), '-');
- }
- public function category()
- {
- return $this->belongsTo(Category::class);
- }
- public function admin()
- {
- return $this->belongsTo(Admin::class);
- }
- }
|