PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 19, 2022

[FIXED] retrieve nested eloquent relations items

 January 19, 2022     laravel, laravel-5, mysql     No comments   

Issue

I have 4 models connected with hasMany relations as below:

Brand -> hasMany -> Categories -> hasMany -> subCategories -> hasMany -> products

I want to retrieve the categories that their subcategories have one or more products.

I tried querying relationship existence with has and/or whereHas but no luck.

Thanx for your advice,


Solution

Using has() dot annotation, should do the trick for you.

$categories = Category::has('subCategories.products')->get();


Answered By - mrhn
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing