Php 从laravel中的一对多关系检索数据属性
我有一对多的关系,如下所示Php 从laravel中的一对多关系检索数据属性,php,laravel,Php,Laravel,我有一对多的关系,如下所示 public function property() { return $this->hasOne(Property::class); } 及 在我的控制器中,我得到如下关系: $property = Property::with('propertycalendars')->get(); return view('users.properties.show',compact('property')); 但考虑到如果我想得到财产的所有权
public function property() {
return $this->hasOne(Property::class);
}
及
在我的控制器中,我得到如下关系:
$property = Property::with('propertycalendars')->get();
return view('users.properties.show',compact('property'));
但考虑到如果我想得到财产的所有权
Property [title] does not exist on this collection instance
我想得到我定义的标题和关系属性,这是我的$property的dd,它显示数据在那里,但我不知道如何通过laravel的show方法访问它
Collection {#1297 ▼
#items: array:1 [▼
0 => Property {#1245 ▼
#connection: "mysql"
#table: "properties"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:12 [▶]
#original: array:12 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"propertycalendars" => Collection {#1296 ▼
#items: array:1 [▼
0 => PropertyCalendar {#1291 ▼
#connection: "mysql"
#table: "property_calendars"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▶]
#original: array:6 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
]
}
这一行:
$property = Property::with('propertycalendars')->get();
它不会获取一个属性,而是将您的所有属性获取到一个集合中。每个属性都有一个标题
您通常会在视图中@foreach这些内容:
@foreach($property as $prop)
{{ $prop->title }}
@endforeach
顺便说一句,为了清楚起见,我将$property重命名为$properties。此行:
$property = Property::with('propertycalendars')->get();
它不会获取一个属性,而是将您的所有属性获取到一个集合中。每个属性都有一个标题
您通常会在视图中@foreach这些内容:
@foreach($property as $prop)
{{ $prop->title }}
@endforeach
顺便说一句,为了清晰起见,我将$property重命名为$properties。这很好,但我如何访问此处的关系?每个属性都将在$prop->propertycalendars中。因为这是一个有很多,你将不得不通过这些foreach,以及。因此,如果您正在显示所有属性及其日历的列表,你可以有两个嵌套的前部。我可以问你,请告诉我怎么才能得到1个属性,就像我说的,它是我的LARAVEL的一个显示方法,考虑我现在在这里的这个链接,所以我展示了1个属性,或者甚至有可能做关于检索单个模型的很多检查。也检查Laravel的路由参数:这很好。我得到了它们,但我如何才能访问这里的关系??每个属性将在$prop->propertycalendars中。因为这是一个有很多,你将不得不通过这些foreach,以及。因此,如果您正在显示所有属性及其日历的列表,你将有两个嵌套的前部。AK OK,我可以请你指导我如何才能获取1个属性,正如我所说的,它是我的LARAVEL的一个显示方法,考虑我现在在这里的这个链接,所以我展示了1个属性,或者甚至有可能在检索单个模型的同时检查Laravel的路由参数: