简体   繁体   English

laravel eloquent 获取所有关系数据

[英]laravel eloquent get all relationship data

I have the following setup in Laravel - a client who can have many reports which contains many rows of data我在 Laravel 中有以下设置 - 一个可以拥有许多包含多行数据的报告的客户端

Client Object (can have many reports)客户端对象(可以有很多报告)

class Client extends Model
{
    public function reports()
    {
      return $this
        ->belongsToMany('App\Report')
        ->withTimestamps();
    }
}

Report Object (can have many rows of data)报表对象(可以有多行数据)

class Report extends Model
{
    public function data()
    {
      return $this
        ->belongsToMany('App\Data', 'report_data')
        ->withTimestamps();
    }
}

Data Object (belongs to a report)数据对象(属于报表)

class Data extends Model
{

    public function report()
    {
      return $this
        ->belongsTo('App\Report')
        ->withTimestamps();
    }

}

If i want to fetch a report for a client i can do this (if i wanted to grab the first one):如果我想为客户获取报告,我可以这样做(如果我想获取第一个报告):

$report = $client -> reports -> first();
var_dump($report -> data); die;

That would give me all the data for the first report这将为我提供第一份报告的所有数据

Or:或者:

foreach($client -> reports as $report){
     var_dump($report -> data);
}

Now the question is - if a client had 3 reports how could i get all the data combined for those 3 reports?现在的问题是 - 如果客户有 3 份报告,我如何才能将这 3 份报告的所有数据组合在一起?

So for example if a client had 3 reports like:例如,如果客户有 3 个报告,例如:

Report 1
Name: test
View: 100
Revenue: 10

Report 2
Name: foo
Views: 50
Revenue: 30

Report 3
Name: bah
Views: 40
Revenue: 20

We could get:我们可以得到:

Name: Total
Views: 190
Revenue: 60

I have tried looping over the reports and merging them into one collection but it doesn't work我试过循环报告并将它们合并到一个集合中,但它不起作用

  $reports = $client -> reports -> all();

  $collection = new \Illuminate\Database\Eloquent\Collection;

  foreach($reports as $report){

      $collection = $collection -> merge($report -> data);
  }

  var_dump($collection);
  die;

The relationship has got me a little stumped这段关系让我有点难受

In Laravel, you can use Eager Loading to get the model with related data.在 Laravel 中,您可以使用Eager Loading来获取具有相关数据的模型。 If you already have the model and want it's related data, you can use Lazy Loading如果你已经有了模型并且想要它的相关数据,你可以使用延迟加载

$client->load('reports.data');

After that you can check the content with之后,您可以检查内容

dd($client);

For getting the total of views, you can do为了获得总观看次数,您可以这样做

$totalViews = $client->reports->sum('views');

Since you will be working with Collections由于您将使用Collections

And you will see the relations field populated with all the reports and, inside them, all the data results for each one.您将看到relations字段中填充了所有报告,其中包含每个报告的所有数据结果。

EDIT编辑

If you want all the data in one place, you can use如果您想将所有数据集中在一个地方,您可以使用

$allData = $client->reports->pluck('data');

Also, have in mind that maybe Laravel hav the attribute data reserved in the models, so if you continue having troubles, try changing that relation name for something like reportData and the model ReportData .另外,请记住,Laravel 可能在模型中保留了属性data ,因此如果您仍然遇到问题,请尝试更改诸如reportData和模型ReportData类的关系名称。

try this尝试这个

$reports = [];
foreach($client -> reports as $report){
     $reports [] = $report->data;
}
var_dump($reports);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM