Category.php 532 B

1234567891011121314151617181920212223242526272829
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Category extends Model
  5. {
  6. protected $fillable = ['name','sort','parent_id'];
  7. //子分类
  8. public function childs()
  9. {
  10. return $this->hasMany('App\Models\Category','parent_id','id');
  11. }
  12. //所有子类
  13. public function allChilds()
  14. {
  15. return $this->childs()->with('allChilds');
  16. }
  17. //分类下所有的文章
  18. public function articles()
  19. {
  20. return $this->hasMany('App\Models\Article');
  21. }
  22. }