Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
922 views
in Technique[技术] by (71.8m points)

php - Getting sub category info in parent category in laravel

I have multi level category where structure is like:

Parent
 - first child
  -- second child
 - another child

What I want to do is, getting products in all child levels in Parent Page so that I can have all products of parent, first child, second child, another child inside Parent.

What I have so far, currently I can get products of Parent, first child & another child but I'm not able to get products of my second child.

Codes

public function totalcategoriessubs($catslug) {
    $category = Category::where('slug','=',$catslug)->with('childs')->first();
    //testing this
    // $products = Product::whereHas('category', function($q) use ($catslug,$category)
    // {
    //   $q->where(function($q) use ($catslug,$category) {
    //     $q->where('slug',$catslug)->orWhere('category_id',$category->id);
    //   });
    // })->orderBy('created_at', 'DESC')->paginate(10);

    $products = Product::whereHas('category', function($q) use ($catslug, $category) {
      $q->where(function($q) use ($catslug,$category) {
        $q->where('slug',$catslug) //works
          ->WhereHas('childs') //works
          ->WhereHas('childs.childs') //not working
          ->orWhere('category_id',$category->id); //works
        });
      })->orderBy('created_at', 'DESC')->paginate(10);
    //end testing
    return view('front.categoriessubs', compact('products', 'category'));
  }

models

Product model

public function category(){
     return $this->belongsTo(Category::class);
}

Category model

public function categories()
  {
    return $this->hasMany(Category::class);
  }

  public function childs() {
    return $this->hasMany(Category::class,'category_id','id') ;
  }

  public function parent()
  {
      return $this->belongsTo(Category::class,'category_id');
  }

  public function isParent()
  {
      return !$this->category_id ? true : false; // if category_id is null => is a Parent Category
  }

  public function products(){
     return $this->hasMany(Product::class);
  }

any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can get the nested childs with simple trick.

only use protected $appends = ['childs', 'products']; in Model.

In Category.php Model

protected appends = ['childs'];


public function categories()
  {
    return $this->hasMany(Category::class);
  }

  public function childs() {
    return $this->hasMany(Category::class,'category_id','id') ;
  }

  public function parent()
  {
      return $this->belongsTo(Category::class,'category_id');
  }

  public function isParent()
  {
      return !$this->category_id ? true : false; // if category_id is null => is a Parent Category
  }

  public function products(){
     return $this->hasMany(Product::class);
  }

Now You can get the Childs using

Category::with('childs')->get();

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...