class PageRelation extends Eloquent
{
public $incrementing = false;
public $timestamps = false;
protected $table = 'page_relation';
protected $casts = [
'parent' => 'int', // FK to page
'child' => 'int', // FK to page
'lpc' => 'int',
];
protected $fillable = [
'lpc',
];
public function children()
{
return $this->hasMany(Page::class, 'category_id', 'child');
}
public function parents()
{
return $this->hasMany(Page::class, 'category_id', 'parent');
}
public function siblings()
{
// ... return $this->hasMany(Page::class ...
// how do I define this relationship?
}
}
Run Code Online (Sandbox Code Playgroud)
在我的设计中, asibling是(如您所料)共享相同parent但不共享自身的记录(不包括 current child)。我怎样才能做到这一点?
这不是Laravel Eloquent Relationships for Siblings的副本,因为 1) 结构不同,2) 我想返回一个关系,而不是一个查询结果,我知道如何查询这个,但我想要 Eager loader 的力量。
我不认为你可以用 Laravel 的内置关系来做到这一点。我建议做的是创建您自己的关系类型来扩展HasMany和使用它。
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class HasManySiblings extends HasMany
{
public function addConstraints()
{
if (static::$constraints) {
if (is_null($foreignKeyValue = $this->getParentKey())) {
$this->query->whereNull($this->foreignKey);
} else {
$this->query->where($this->foreignKey, '=', $foreignKeyValue);
$this->query->whereNotNull($this->foreignKey);
}
$this->query->where($this->localKey, '!=', $this->parent->getAttribute($this->localKey));
}
}
public function getParentKey()
{
return $this->parent->getAttribute($this->foreignKey);
}
}
Run Code Online (Sandbox Code Playgroud)
通过扩展HasMany类并提供您自己的实现,addConstraints您可以控制添加到相关模型查询中的内容。通常,Laravel 在这里会做的是 addwhere parent_id = <your model ID>但我在这里将其更改为 add where parent_id = <your model PARENT ID>(如果您的模型parent_id是null它,它将改为 add where parent_id is null)。我还添加了一个额外的条款,以确保呼叫模型不包括所产生的集合:and id != <your model ID>。
您可以在Page模型中像这样使用它:
class Page extends Model
{
public function siblings()
{
return new HasManySiblings(
$this->newRelatedInstance(Page::class)->newQuery(), $this, 'parent_id', 'id'
);
}
}
Run Code Online (Sandbox Code Playgroud)
现在你应该能够像这样加载兄弟姐妹:
$page = Page::find(1);
dd($page->siblings);
Run Code Online (Sandbox Code Playgroud)
不过请注意,我只测试了这个以检索相关模型,当将关系用于其他目的(例如保存相关模型等)时,它可能不起作用。
另外,请注意,在我上面的示例中,我使用了parent_id而不是parent您的问题。不过应该是直接交换。
| 归档时间: |
|
| 查看次数: |
1615 次 |
| 最近记录: |