Self Join in Eloquent

How would you write self-participation in eloquence? Do I need to define relationships on a model?

Here is my statement:

SELECT t2.title FROM products t1, products t2 WHERE t1.id = $id AND t2.color_id = t1.color_id AND t2.id != $id 
+6
source share
1 answer

You can simply determine your attitude to yourself.

 public function parent() { return $this->belongsTo(self::class, 'color_id'); } public function children() { return $this->hasMany(self::class, 'color_id'); } 
+6
source

Source: https://habr.com/ru/post/988379/


All Articles