我有两个关系的模型。我想在每个关系上添加 where 条件。
例如,告诉我一个房间,日期4/11/2019
和城市london
控制器:
$test = Property::with('dates','details')->get();
$测试结果:
它可能有点长,但我扩展了整个结果,以便您可以检查关系,因为日期处于枢轴关系中:
Collection {#1708 ▼
#items: array:2 [▼
0 => Property {#1457 ▼
#guarded: []
#connection: "mysql"
#table: "properties"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:8 [▶]
#original: array:8 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:2 [▼
"dates" => Collection {#1607 ▼
#items: array:1 [▼
0 => Date {#1600 ▼
#connection: "mysql"
#table: "dates"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▶]
#original: array:9 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"pivot" => Pivot {#1602 ▼
+incrementing: false
#guarded: []
#connection: null
#table: "date_property"
#primaryKey: "id"
#keyType: "int"
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [▶]
#original: array:2 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: []
+pivotParent: Property {#1461 ▶}
#foreignKey: "property_id"
#relatedKey: "date_id"
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
]
}
"details" => PropertyDetail {#1702 ▼
#fillable: array:7 [▶]
#connection: "mysql"
#table: "property_details"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▶]
#original: array:10 [▼
"id" => 52
"property_id" => 65
"state" => "london"
"city" => "london"
"address" => "5"
"post_code" => 5
"placearea" => 1
"telephone" => 5
"created_at" => "2019-04-09 21:03:10"
"updated_at" => "2019-04-09 21:03:10"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
1 => Property {#1458 ▶}
]
}