Closed SilverKenn closed 3 years ago
I'll look into it.
This is a special kind of relationship and not supported yet with withCount()
/has()
/whereHas()
: The fact that the parent table (users
) also appears as an intermediate table requires custom treatment.
I'll work on a fix.
Hey, Thank you so much for looking into this, really appreciate if we can have it working with this special relationship I have :)
BTW, do you have a donation button? Ive been searching through your packages and referred site finding a way to buy you a beer :)
Thank you very much, I appreciate it! I've added a donation button to my repositories: https://paypal.me/JonasStaudenmeir
You can temporarily fix the issue by adding an alias to the intermediate table:
class User extends BaseModel {
use \Staudenmeir\EloquentHasManyDeep\HasRelationships,
\Staudenmeir\EloquentHasManyDeep\HasTableAlias;
public function childApplications() {
return $this->hasManyDeep(
Application::class,
[ User::class.' as alias', Applicant::class, 'applicant_application'],
['parent_id']
);
}
}
Thank you very much, working perfectly now even with using withCount()
I released a new version that handles this issue. Unfortunately, the package can't completely fix it, but now detects it and explains what to do. The package can set the alias itself, but still requires the user to add the HasTableAlias
trait to the affected model.
Hello,
Thank you so much for this very helpful package, really powerful 👍
I'm having issue though where eager loading count produce SQL error.
This is my basic data structure User has parent_id column which is targeted to its own user table through id Applicant has user_id foreign key column Applicant and Application has many-to-many relationship
this is what Im try to achieve parent user → hasmany children user → hasmany applicant → belongsToMany applications
here is my User model
All of this methods are working fine when I eager load them using
with()
however,childApplications
method doesn't work onwithCount
This one is working fine and returns proper collections
I can see the sql query working fine as well
But this one doesn't work and I can see the sql query is a mess
the above code produces an error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'applications.user_id' in 'on clause'
which I understand as its trying to access non-existing column.below is the sql query when using
withCount
, its joining theusers
table directly and skipping theapplicant_application
andapplicants
table unlike the query with usingwith()
Would appreciate your guidance and help on where I miss something