你会如何写一个self join in eloquent?我需要在模型上定义关系吗?
这是我的声明:
SELECT t2.title FROM products t1, products t2
WHERE t1.id = $id
AND t2.color_id = t1.color_id AND
t2.id != $id
最佳答案
您可以简单地定义与自身的关系。
public function parent()
{
return $this->belongsTo(self::class, 'color_id');
}
public function children()
{
return $this->hasMany(self::class, 'color_id');
}
关于laravel - self 加入 Eloquent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30592793/