dpthuyh1678 2016-10-12 20:33
浏览 52

Laravel Eloquent多个表格

I have a complex setup which includes the following tables.

Customers
Transactions
Jobs
Rounds
Job_Rounds

I want to output all the customers which are overdue. I've done that by using the following query:

$data = Auth::user()->clients()->leftjoin('transactions','clients.id','=','transactions.client_id')
                ->select(DB::raw('sum(gross) as value, email, first_name, last_name, mobile, clients.id, addressline1'))
                ->groupBy('id','first_name','last_name','email','mobile','addressline1', 'clients.id')
                ->havingRaw('SUM(gross) < 0')
                ->get();

That returns all the customers which are overdue which is great, however I now want to be able to filter the overdue customers by round.

My relationships are as follows:

Customers > Jobs.client_id
Customers > Transactions.client_id
Jobs > Rounds via Jobs_Rounds

I would pass to my controller the round_id of a given round and then try to filter the results based on the round_id. the only place I'm storing the round ID is in the jobs_rounds table and that table only contains job_id & round_id.

  • 写回答

1条回答 默认 最新

  • douwei2966 2016-10-12 20:45
    关注

    I'm thinking about using the HasManyThrough relationship to link a Customer and a Round.

    Something like this :

    // Customer.php
    public function rounds()
    {
        return $this->hasManyThrough(Round::class, Job::class, 'client_id', 'job_id', 'id');
    }
    

    Then in your controller you could try this :

    $data = Auth::user()->clients()
                ->whereHas('rounds', function ($query) {
                    $query->where('id', request()->input('round_id'))
                })
                //... the rest of your filters
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题