English 中文(简体)
从相关表格中得出多行的雄辩。
原标题:laravel eloquent with sum multiple rows from linked table

我有一个表<代码>Quotes,与QuoteDetails表有着一对一的关系。

表格结构如下:

return $this->quotes->with( quotedetails )
    ->orderBy( created_at ,  DESC )->paginate(10);

成果:

{
   "current_page":1,
   "data":[
      {
         "id":2,
         "subject":"demo",
         "quotedetails":[
            {
               "id":2,
               "quotes_id":2,
               "description":"testing data",
               "total":1080
            },
            {
               "id":3,
               "quotes_id":2,
               "description":"testing",
               "total":180
            }
         ]
      },
      {
         "id":1,
         "subject":"demo",
         "quotedetails":[
            {
               "id":1,
               "quotes_id":1,
               "description":"data",
               "total":360
            }
         ]
      }
   ],
   "per_page":10,
   "prev_page_url":null,
   "to":2,
   "total":2
}

我想列入<代码>引述细节>的总和<代码>。 页: 1 我不肯定应该如何做到这一点。

在这方面,我是如何在我的模型(Quotes)中查询的。

public function quotedetails(){
        return $this->hasMany( AppModelsQuotesDetail ,  quotes_id ,  id );
    }

如何获得<代码>总。

www.un.org/Depts/DGACM/index_spanish.htm 这里是预期产出()的方法。

{
   "current_page":1,
   "data":[
      {
         "id":2,
         "subject":"demo link data",
         "quotesum": 1260
      },
      {
         "id":1,
         "subject":"demo",
         "quotesum": 360
      }
   ],
   "per_page":10,
   "prev_page_url":null,
   "to":2,
   "total":2
} 
最佳回答
$query->withCount([
 quotedetail AS quotesum  => function ($query) {
            $query->select(DB::raw("SUM(total) as quotesum"));
        }
    ]);
问题回答

我的理解是,你希望通过询问这样做,但是,如果不问你,你就能够以这种方式改变稀有数据,举以下例子:

$quotes = Quotes::whereNotNull( id )->with( quotedetails )
    ->orderBy( created_at ,  DESC )->paginate();

//transform the data in the Paginate instance
$quotes->getCollection()->transform(function($quote){
     $quote->quotesum = $quote->quotedetails->sum( total );
     unset($quote[ quotedetails ]);
     return $quote;
});

p化结果中的数据已经改变,而且你也要求这样做。

<>Pro: 在不增加任何疑问的情况下,有更多的机会与你的答复做更多的工作。

<><>Con: 因此,更多的数据意味着在收集方面开展更多的工作,特别是在数据大的情况下。

Hi you use eloquent itself to archive this. You can use with sum method to calculate the sum of total column from quotedetails table $ideas = Quotes::withSum( quotedetails , total )->with( quotedetails )->all();





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签