Php Laravel刀片获取远程关系数据
我有一个相册模型,其中有许多图像,我可以在控制器中获取这些图像(并将其传递给视图),如下所示: 在数据上执行var_转储,它似乎正在按预期工作:Php Laravel刀片获取远程关系数据,php,laravel,Php,Laravel,我有一个相册模型,其中有许多图像,我可以在控制器中获取这些图像(并将其传递给视图),如下所示: 在数据上执行var_转储,它似乎正在按预期工作: images: (object) Illuminate\Database\EloquentCollection [Object ID: 377][Number of Properties: 1] items:protected: (array) [Number of elements: 5] 0: (object) AppImage [Objec
images:
(object) Illuminate\Database\EloquentCollection [Object ID: 377][Number of Properties: 1]
items:protected:
(array) [Number of elements: 5]
0:
(object) AppImage [Object ID: 382][Number of Properties: 24]
table:protected: (string) image
connection:protected: (null) NULL
primaryKey:protected: (string) id
keyType:protected: (string) int
incrementing: (boolean) true
with:protected:
(array) [Number of elements: 0]
perPage:protected: (integer) 15
exists: (boolean) true
wasRecentlyCreated: (boolean) false
attributes:protected:
(array) [Number of elements: 6]
id: (integer) 1
filename: (string) vAIsX53hlkRUSkmohe0SyeEhEzkDTQnsQxrL3DKt.png
path: (string) images/20170322000000
album_id: (integer) 1
created_at: (string) 2017-03-22 16:02:55
updated_at: (string) 2017-03-22 16:02:55
original:protected:
(array) [Number of elements: 6]
id: (integer) 1
filename: (string) vAIsX53hlkRUSkmohe0SyeEhEzkDTQnsQxrL3DKt.png
path: (string) images/20170322000000
album_id: (integer) 1
created_at: (string) 2017-03-22 16:02:55
updated_at: (string) 2017-03-22 16:02:55
我在相册上循环并打印出标题,效果很好,但是当尝试访问相册对象中的图像对象时,我没有得到任何输出:
@foreach ($albums as $album)
{{ $album->name }} <br>
@foreach ($album->images() as $image)
{{ $image->filename }}
@endforeach
@endforeach
@foreach($albums作为$album)
{{$album->name}}
@foreach($album->images()作为$image)
{{$image->filename}
@endforeach
@endforeach
{{$album->name}}
按预期工作,但我没有输出{{{$image->filename}
我是否试图错误地访问图像文件名?将关系作为方法调用时,它返回查询生成器实例。您需要将其更改为属性调用以获取图像集合
所以改变
@foreach ($album->images() as $image)
到
当您将关系作为方法调用时,它将返回一个查询生成器实例。您需要将其更改为属性调用以获取图像集合 所以改变
@foreach ($album->images() as $image)
到
里面是一个数组。我想应该是
$image['filename']
。试一试?@Thamilan$album->images()
实际上是内部的查询生成器对象。一旦他们将其更改为$album->images
,它将成为内部的模型实例。因为模型实现了ArrayAccess接口,所以可以使用$image->filename
或$image['filename']
访问属性。两者都有效且正确。它是内部的数组。我想应该是$image['filename']
。试一试?@Thamilan$album->images()
实际上是内部的查询生成器对象。一旦他们将其更改为$album->images
,它将成为内部的模型实例。因为模型实现了ArrayAccess接口,所以可以使用$image->filename
或$image['filename']
访问属性。两者都是有效和正确的。
@foreach ($album->images as $image)