拉拉维尔的关系检索差异



两者有什么区别

$this->translations;

$this->translations()->get();

其中翻译是当前模型的关系。我以为两者都做同样的事情。

它们基本上是一回事。

然而:

  • 每次调用此函数时,$this->translations()->get();都会查询数据库。
  • $this->translations;将首次执行数据库查询,并将结果存储在内存中供以后使用。

$this->translations;会将所有翻译返回到相关模型, 而$this->translations()将为您提供查询构建器实例,您可以在其中将约束添加到相关模型中,例如

$this->translations()->where(['locale' , 'en'])->orderBy('sort')->get();

最新更新