为什么 - 模型方法中的最新方法读取相关表中的所有行?

发布于 01-22 04:34 字数 678 浏览 2 评论 0原文

获得了最新价值,

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱她像谁2025-01-29 04:34:16

查询是正确的。当您急切地加载使用使用方法的货币 的关系时,您将加载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 load currency_histories for all of your Currency 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文