Category.php 739 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. namespace App\Models;
  3. class Category extends Base
  4. {
  5. /**
  6. * 删除数据
  7. *
  8. * @param array $map
  9. * @return bool
  10. */
  11. public function destroyData($map)
  12. {
  13. // 先获取分类id
  14. $categoryIdArray = $this
  15. ->whereMap($map)
  16. ->pluck('id')
  17. ->toArray();
  18. // 获取分类下的文章数
  19. $articleCount = Article::whereIn('category_id', $categoryIdArray)->count();
  20. // 如果分类下存在文章;则需要下删除文章
  21. if ($articleCount !== 0) {
  22. flash_message('请先删除此分类下的文章', false);
  23. return false;
  24. }
  25. // 删除分类
  26. return parent::destroyData($map);
  27. }
  28. }