Lumen / Laravel Eloquent hasManyThrough嵌套结果

时间:2017-08-22 21:10:18

标签: php laravel eloquent has-many-through lumen

我正在尝试使用Laravel Eloquent实现嵌套结果。

我的逻辑: 城市,社区,街区

一个城市有很多社区

一个社区有很多街区

我想要完成的结果:

use Term::ProgressBar::IO;

open my $fh, '<', 'passwords.txt' or die "could not open file n.txt: $!";
my $pb = Term::ProgressBar::IO->new($fh);
my $line_count;
while (<$fh>) {
  $line_count += 1;
  $pb->update();
}
close $fh;
print "total lines $line_count"


C:\Users\USER\Desktop>r.pl
  0% [*                                                                                                                                                               ]total lines 360

数据应该是:

City
 Neighbourhoods
  Blocks

型号:

City.php

[
  {

    id: 1,
    name: "New York",
    neighbourhoods: [
      {
        id: 1,
        name: "Neighbourhood 1",
        city_id: 1,
        blocks: [
          {
            id: 1,
            name: "Neighbourhood 1 Block 1",
            neighbourhood_id: 1
          },
          {
            id: 2,
            name: "Neighbourhood 1 Block 2",
            neighbourhood_id: 1
          }
        ]
      },
      {
        id: 2,
        name: "Neighbourhood 2",
        city_id: 1,
        blocks: [
          {
            id: 3,
            name: "Neighbourhood 2 Block 1",
            neighbourhood_id: 2
          },
          {
            id: 4,
            name: "Neighbourhood 2 Block 2",
            neighbourhood_id: 2
          }
        ]
      }
    ]
  }
]

Neighbourhood.php

public function neighbourhood()
{
    return $this->hasMany('App\Neighbourhood');
}

public function block()
{
    return $this->hasManyThrough('App\Block', 'App\Neighbourhood', 'city_id', 'neighbourhood_id', 'id');
}

Block.php

public function city()
{
    return $this->belongsTo('App\City');
}

public function block()
{
    return $this->hasMany('App\Block');
}

实际结果给了我这个:

public function neighbourhood()
{
    return $this->belongsTo('App\Neighbourhood');
}

我希望嵌套结果。有没有办法做到这一点?我错过了什么?

当然,我可以用PHP foreach循环完成,但这将是手动工作。我想知道我是否可以直接从查询中获得结果。

谢谢。

SOLUTION:

[
    {
        "id": 1,
        "name": "Ney York",
        "neighbourhoods": [
            {
                "id": 1,
                "city_id": 1,
                "name": "Heighbourhood 1"
            },
            {
                "id": 2,
                "city_id": 1,
                "name": "Heighbourhood 2"
            },
            {
                "id": 4,
                "city_id": 1,
                "name": "Păcurari"
            }
        ],
        "blocks": [
            {
                "id": 1,
                "name": "Heighbourhood 1 Block 1",
                "neighbourhood_id": 1,
                "city_id": 1
            },
            {
                "id": 2,
                "name": "Heighbourhood 1 Block 2",
                "neighbourhood_id": 1,
                "city_id": 1
            }
        ]
    }
]

1 个答案:

答案 0 :(得分:1)

这应该做:

$result = City::with('neighbourhoods.blocks')->get();

您可以使用点表示法执行嵌套relationship queries