1

My query mysql like this :

SELECT * 
FROM categories a
JOIN categories b ON b.parent_id = a.id
JOIN products c ON c.category_id = b.id
WHERE a.id = 1

I want change it to laravel eloquent

On the model category, I try like this :

self::join('categories b', 'b.parent_id', '=', 'a.id')
    ->join('products c', 'c.category_id', '=', 'b.id')
    ->findWhere(['a.id','=',$id]);

I'm confused add alias to self

How can I do it?

Note :

I want to use self. Not others

Shadow
  • 33,525
  • 10
  • 51
  • 64
samuel toh
  • 6,836
  • 21
  • 71
  • 108
  • _**@Shadow**_: Did referred post really say anything about _**alias**_ for _**self**_ or anything else? – Ravinder Reddy Mar 17 '17 at 12:09
  • @Shadow, Seems this is no duplicate. My case is different. You should read my case slowly – samuel toh Mar 17 '17 at 12:43
  • @RavinderReddy well, both you and the OP should read the duplicate post slowly. It describes how to create a self join in laravel by setting up a relationship with itself. This is what you need to do here as well. – Shadow Mar 17 '17 at 12:48

1 Answers1

0

Since you're gonna do it in the model. I think you need to use $this instead of self. Just like what you did in the making of table relation :

return $this->hasMany('Blabla');
Aryo Pradipta Gema
  • 1,190
  • 3
  • 13
  • 29