Category.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App;
  5. class Category extends Model
  6. {
  7. protected $with = ['category_translations'];
  8. public function getTranslation($field = '', $lang = false){
  9. $lang = $lang == false ? App::getLocale() : $lang;
  10. $category_translation = $this->category_translations->where('lang', $lang)->first();
  11. return $category_translation != null ? $category_translation->$field : $this->$field;
  12. }
  13. public function category_translations(){
  14. return $this->hasMany(CategoryTranslation::class);
  15. }
  16. public function products(){
  17. return $this->hasMany(Product::class);
  18. }
  19. public function classified_products(){
  20. return $this->hasMany(CustomerProduct::class);
  21. }
  22. public function categories()
  23. {
  24. return $this->hasMany(Category::class, 'parent_id');
  25. }
  26. public function childrenCategories()
  27. {
  28. return $this->hasMany(Category::class, 'parent_id')->with('categories');
  29. }
  30. public function parentCategory()
  31. {
  32. return $this->belongsTo(Category::class, 'parent_id');
  33. }
  34. public function attributes()
  35. {
  36. return $this->belongsToMany(Attribute::class);
  37. }
  38. }