减少前面的sql查询次数

发布于 2025-01-10 11:50:44 字数 1023 浏览 1 评论 0原文

    orders
        id - integer
        client_id - integer
     
    clients
        id - integer
        name - string
     
    accounts
        id - integer
        client_id - integer
        amount - integer

控制器

$orders = Order::with(['transaction', 'client', 'delivery', 'address'])
  ->latest()->paginate(50);
return view('admin.order.index', compact('orders'));

前端

      <td class="text-center">
        <strong>{{$item->client->invoice}}</strong>
      </td>

客户端模型

  public function getInvoiceAttribute()
  {
    return $this->account()->sum('amount');
  }

在此处输入图像描述

我不知道如何使用有很多通过。 或者说遇到这种情况怎么解决 我不需要前台账户,但我需要金额总和

    orders
        id - integer
        client_id - integer
     
    clients
        id - integer
        name - string
     
    accounts
        id - integer
        client_id - integer
        amount - integer

Controller

$orders = Order::with(['transaction', 'client', 'delivery', 'address'])
  ->latest()->paginate(50);
return view('admin.order.index', compact('orders'));

FrontEnd

      <td class="text-center">
        <strong>{{$item->client->invoice}}</strong>
      </td>

Client Model

  public function getInvoiceAttribute()
  {
    return $this->account()->sum('amount');
  }

enter image description here

I don't know how to use Has Many Through.
Or how to solve this situation
I don't need an account at the front but I need sum of amounts

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

在你怀里撒娇 2025-01-17 11:50:45

我这样做了,

之后写入帐户总和 -> 金额

Schema::table('clients', function (Blueprint $table) {
          $table->integer('invoice')->default(0);
        });

添加了发票列,以在添加 AccountObserver

class AccountObserver
{
  public function creating(Account $account)
  {
    $account->client()->increment('invoice',$account->amount);
  }

  public function updating(Account $account)
  {
    $account->client()->increment('invoice',$account->amount);
  }
} 

控制器

$orders = Order::with(['transaction', 'client', 'delivery', 'address'])
  ->latest()->paginate(50);
return view('admin.order.index', compact('orders'));
视图
<td class="text-center">
    <strong >{{$item->client->invoice}}</strong>
</td>

在此处输入图像描述

I did so

added invoice column to write sum of account->amounts

Schema::table('clients', function (Blueprint $table) {
          $table->integer('invoice')->default(0);
        });

after than added AccountObserver

class AccountObserver
{
  public function creating(Account $account)
  {
    $account->client()->increment('invoice',$account->amount);
  }

  public function updating(Account $account)
  {
    $account->client()->increment('invoice',$account->amount);
  }
} 

Controller

$orders = Order::with(['transaction', 'client', 'delivery', 'address'])
  ->latest()->paginate(50);
return view('admin.order.index', compact('orders'));
View
<td class="text-center">
    <strong >{{$item->client->invoice}}</strong>
</td>

enter image description here

月棠 2025-01-17 11:50:45
$this->account()->sum('amount');

这将创建一个 SQL 查询。如果你在 foreach 循环中调用它,它将执行 N 次查询。

您可以急切地加载总和。

$orders = Order::query()
    ->with([
        'transaction',
        'client' => fn ($client) => $client->withSum('accounts as invoice', 'amount'), // or function ($client) { $client->withSum('accounts as invoice', 'amount'); }, 
        'delivery',
        'address'
    ])
    ->latest()
    ->paginate(50);

return view('admin.order.index', compact('orders'));
@foreach ($orders as $item)
  ...
  <td class="text-center">
    <strong>{{ $item->client->invoice }}</strong>
  </td>
  ...
@endforeach

Client 模型中删除 getInvoiceAttribute 方法

$this->account()->sum('amount');

This creates a SQL query. If you call it in a foreach loop, it will do N Queries.

You could eager load the sum.

$orders = Order::query()
    ->with([
        'transaction',
        'client' => fn ($client) => $client->withSum('accounts as invoice', 'amount'), // or function ($client) { $client->withSum('accounts as invoice', 'amount'); }, 
        'delivery',
        'address'
    ])
    ->latest()
    ->paginate(50);

return view('admin.order.index', compact('orders'));
@foreach ($orders as $item)
  ...
  <td class="text-center">
    <strong>{{ $item->client->invoice }}</strong>
  </td>
  ...
@endforeach

Remove the getInvoiceAttribute method from the Client Model

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