为什么 - 模型方法中的最新方法读取相关表中的所有行?
获得了最新价值,
$currencies = Currency
::getByActive(true)
->withCount('currencyHistories')
->with('latestCurrencyHistory')
->orderBy('ordering', 'asc')
->get();
在Laravel 9中,我在Model App/Model/Currency.php中
public function latestCurrencyHistory()
{
return $this->hasOne('App\Models\CurrencyHistory')->latest();
}
我有:但是检查生成的SQL我看到的行类似于:
SELECT *
FROM `currency_histories`
WHERE `currency_histories`.`currency_id` in (8, 13, 16, 19, 27, 30)
ORDER BY `created_at` desc
我想此代码是通过最新含量象征的方法提出的 我在这里设置了一些限制条件,因为结果数据太大了。
谢谢!
In laravel 9 I got last value in related CurrencyHistory table
$currencies = Currency
::getByActive(true)
->withCount('currencyHistories')
->with('latestCurrencyHistory')
->orderBy('ordering', 'asc')
->get();
In model app/Models/Currency.php I have :
public function latestCurrencyHistory()
{
return $this->hasOne('App\Models\CurrencyHistory')->latest();
}
But checking generated sql I see lines like :
SELECT *
FROM `currency_histories`
WHERE `currency_histories`.`currency_id` in (8, 13, 16, 19, 27, 30)
ORDER BY `created_at` desc
I suppose this code is raised by latestCurrencyHistory method and wonder can
I set some limit 1 condition here, as resulting data are too big.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

查询是正确的。当您急切地加载使用使用方法的货币 的关系时,您将加载
Currency_histories
用于所有crumend> crurners
>集合中的模型。如果您转储结果,则将有具有IDS的货币:8、13、16、19、27、30和一个
最新含量象征
(如果存在)。Query is correct. As you eager load your relation for the collection of currencies using
with
method, you loadcurrency_histories
for all of yourCurrency
models in collection.If you dump the result, you will have currencies with IDs: 8, 13, 16, 19, 27, 30 and one
latestCurrencyHistory
(if present) for each.