I have model with 2 relationships. I want to add where condition on each relationship.
For example show me a room that date is 4/11/2019
and the city in london
Controller:
$test = Property::with('dates','details')->get();
$test result:
It may be a bit long but I expanded whole result so you can check the relations as dates is in a pivot relation :
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 ▶}
]
}