Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何访问Laravel中的嵌套关系数据_Php_Laravel_Eloquent_Relationship - Fatal编程技术网

Php 如何访问Laravel中的嵌套关系数据

Php 如何访问Laravel中的嵌套关系数据,php,laravel,eloquent,relationship,Php,Laravel,Eloquent,Relationship,使用Laravel 5.4,以及 模型关系: Painter (has many) Paintings Hall (has many) Paintings $halls = Hall::with(paintings.painter)->get(); public function getPaintersAttribute() { return \App\Painter::select('painters.*') ->join('paintings', 'painter

使用Laravel 5.4,以及

模型关系:

Painter (has many) Paintings
Hall (has many) Paintings
$halls = Hall::with(paintings.painter)->get();
public function getPaintersAttribute() {
  return \App\Painter::select('painters.*')
    ->join('paintings', 'painter_id', 'painters.id')
    ->where('paintings.hall_id', $this->id)
    ->groupBy('painter.id')
    ->orderBy('name')
    ->get();
}
反过来说:

Painting (belongs to one) Painter
Painting (belongs to one) Hall
霍尔控制器索引()获取集合:

Painter (has many) Paintings
Hall (has many) Paintings
$halls = Hall::with(paintings.painter)->get();
public function getPaintersAttribute() {
  return \App\Painter::select('painters.*')
    ->join('paintings', 'painter_id', 'painters.id')
    ->where('paintings.hall_id', $this->id)
    ->groupBy('painter.id')
    ->orderBy('name')
    ->get();
}
霍尔模型有一个供画家使用的存取器:

Painter (has many) Paintings
Hall (has many) Paintings
$halls = Hall::with(paintings.painter)->get();
public function getPaintersAttribute() {
  return \App\Painter::select('painters.*')
    ->join('paintings', 'painter_id', 'painters.id')
    ->where('paintings.hall_id', $this->id)
    ->groupBy('painter.id')
    ->orderBy('name')
    ->get();
}
在模板中:

{{ $hall->painters->count() }}

@foreach ($hall->painters as $painter)
    painter: {{ $painter->name }}
@endforeach
这是可行的,但是

有两个数据库查询嵌套的急切加载关系:

SELECT * FROM 'paintings' WHERE hall_id IN ( <list of hall_id's> )

SELECT * FROM 'painters' WHERE painter_id IN ( <list of painter_id's for that hall> )
hasManyThrough不起作用,因为这种关系不起作用 霍尔有油漆工

我可以将一个变量与数据一起从控制器传递到视图,但这样做似乎很奇怪,因为嵌套关系已经被加载了

是否有其他方法可以访问视图中作为集合的嵌套数据