123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace App\Repositories\Models\Exam;
- use App\Repositories\Models\Model;
- use Illuminate\Support\Facades\DB;
- class BankCategory extends Model
- {
- /**
- * @var string
- */
- protected $table = 'exam_bank_categories';
- protected $guarded = [];
- /**
- * The attributes excluded from the model's JSON form.
- *
- * @var array
- */
- protected $hidden = [];
- public static function boot()
- {
- parent::boot(); // TODO: Change the autogenerated stub
- self::updated(function (BankCategory $bankCategory) {
- if ($bankCategory->isDirty('pid')) {
- $nodes = self::byIdGetParentIds($bankCategory->id);
- $nodes = '-' . arr2str($nodes, '-') . '-';
- DB::table('exam_bank_categories')->where('id', $bankCategory->id)->update(['nodes' => $nodes]);
- }
- });
- }
- public function parent()
- {
- return $this->belongsTo(self::class, 'pid');
- }
- public function children()
- {
- return $this->hasMany(self::class, 'pid');
- }
- public function setNodesAttribute($val)
- {
- $val = arr2str($val, '-');
- $this->attributes['nodes'] = "-{$val}-";
- }
- public static function byIdGetParentIds($id)
- {
- $ids = [$id];
- $pid = self::query()->where('id', $id)->value('pid');
- if ($pid) {
- $pid = self::byIdGetParentIds($pid);
- $ids = array_merge($ids, $pid);
- }
- return $ids;
- }
- }
|