提取数据总结金额列,以使结果由客户,月和年分组
我有一个客户表和一个客户交易表。
这是下面的客户表。
这是下面的客户交易表。
这是我想要实现的目标:
我希望能够对金额列进行求和,以便结果按客户分组,并且月份和年份。
这就是我到目前为止所做的:
我为客户和客户交易创建了一个模型。我在客户事务模型中具有这种关系:
public function customer()
{
return $this->belongsTo(Customer::class);
}
我还有一个客户控制器,其中有获取数据的逻辑。在我的 CustomerController 文件中:
public function index()
{
$transactions = CustomerTransaction::with('customer')->get();
dd($transactions);
}
I have a customers table and a customer transactions table.
This is the customers' table below.
This is the customer transactions table below.
This is what I want to achieve below:
I want to be able to sum up the amount column such that the result is grouped by customers and month and year.
This is what I've done so far:
I created a model for both Customers and Customers transactions. I have this relationship in the Customer transactions model:
public function customer()
{
return $this->belongsTo(Customer::class);
}
I also have a Customer controller where I have the logic to fetch the data. In my CustomerController file:
public function index()
{
$transactions = CustomerTransaction::with('customer')->get();
dd($transactions);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能够解决它。在 customercontroller 文件中,这就是我现在在索引方法中所拥有的:
然后,在我看来,我有:
I was able to resolve it. In the CustomerController file, this is what I now have in the index method:
Then in my view, I have this:
我能够编写SQL查询:
I was able to write the SQL query: