[英]how to use queries on 2 relationships in laravel
I have model with 2 relationships. 我有2关系的模型。 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
例如,给我显示一个日期为4/11/2019
月11日的房间,以及london
的城市
Controller: 控制器:
$test = Property::with('dates','details')->get();
$test result: $ test结果:
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 ▶}
]
}
You can do something like this, 你可以做这样的事情,
$data = Property::with(['dates' => function ($query) {
$query->where('datefield', 'like', '4/11/2019'); // datefield I ain't saw in your output, you can replace it
}],['details' => function ($query) {
$query->where('city', 'like', 'london');
}])->get();
dd($data);
Refer documentation how to use it here . 请在此处参阅文档,了解如何使用它。
I hope your date format in the table is as m/d/Y
if not you have to follow below steps. 我希望表格中的日期格式为m/d/Y
,否则,您必须执行以下步骤。
$date = date("Y-m-d",strtotime(str_replace("/","-",$yourdate)));
You can use $date
variable in place of 4/11/2019
. 您可以使用$date
变量代替4/11/2019
。
Note: Dates in the m/d/y or dmy formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; 注意:通过查看各个组成部分之间的分隔符,可以消除m / d / y或dmy格式的日期的歧义:如果该分隔符是斜杠(/),则假定使用美国m / d / y; whereas if the separator is a dash (-) or a dot (.), then the European dmy format is assumed. 相反,如果分隔符是破折号(-)或点(。),则采用欧洲dmy格式。 If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as ymd. 但是,如果以两位数字格式给出年份,并且分隔符为破折号(-,则日期字符串将解析为ymd。
EDIT 编辑
$property = Property::with(['dates' => function ($query) {
$query->where('datefield', 'like', '4/11/2019');
}])->get();
May be you can try 也许你可以尝试
$property = Property::with(['dates' => function ($query) {
$query->whereDate('datefield', '4/11/2019');
}])->get();
and you do not need LIKE
. 并且您不需要LIKE
。 See documentation , I am not saying LIKE
will not work, but using a =
or whereDate
will be more accurate. 请参阅文档 ,我并不是说LIKE
无效,但是使用=
或whereDate
会更准确。
Can you try 你能试一下吗
$data = Property::with(['dates' => function ($query) {
$query->whereDate('your_date_field', '=', '4/11/2019');
}],['details' => function ($query) {
$query->where('city', 'london');
}])->get();
or 要么
$data = Property::whereHas('dates', function($query){
$query->where('your_date_field', '4/11/2019');
})->whereHas('city', function($query){
$query->where('city', 'london');
})->get();
If you want to filter the entities by conditions on their relations, then you need whereHas()
(see Querying Relationship Existence ): 如果whereHas()
实体之间的关系条件过滤实体,则需要whereHas()
(请参阅查询关系存在 ):
$searchDate = '2019-04-11';
$searchCity = 'london';
$test = Property::with('dates','details')
->whereHas('dates', function($query) use($searchDate) {
$query->where('date', $searchDate);
})
->whereHas('details', function($query) use($searchCity) {
$query->where('city', $searchCity);
})
->get();
If you also want to filter the returned relations by the same condition, then you can do it within with()
(see Constraining Eager Loads ): 如果您还想按相同条件过滤返回的关系,则可以在with()
(请参见约束急切的负载 ):
$test = Property::with(['details', 'dates' => function($query) use($searchDate) {
$query->where('date', $searchDate);
}])
->whereHas('dates', function($query) use($searchDate) {
$query->where('date', $searchDate);
})
->whereHas('details', function($query) use($searchCity) {
$query->where('city', $searchCity);
})
->get();
You only need to do that for dates
, since there can be only one details
relation, which is already constrained to 'london'
. 您只需要对dates
执行此操作,因为只能存在一个details
关系,该关系已被约束为'london'
。
Most important thing is Date field and City should be use equal condition not like
condition. 最重要的是日期字段,城市应该是用同等条件不like
的状态。
Notes : You should check your date format for proper data output. 注意:您应该检查日期格式以获取正确的数据输出。
$data = Property::with(['dates' => function ($query) {
$query->where('your_date_field', '=', '4/11/2019');
}],['details' => function ($query) {
$query->where('city', '=', 'london');
}])->get();
You can also use whereDate()
function to compare date field. 您还可以使用whereDate()
函数比较日期字段。
$data = Property::with(['dates' => function ($query) {
$query->whereDate('your_date_field', '4/11/2019');
}],['details' => function ($query) {
$query->where('city', '=', 'london');
}])->get();
You can try this : 您可以尝试以下方法:
$data = Property::with([
'dates' => function ($query) {
$query->whereDate('your_date_field', 'formatted_date');
},
'details' => function ($query) {
$query->where('city', '=', 'london');
}
])->get();
If you need details of property only, not the relation data, then you can try for : 如果仅需要属性的详细信息,而不是关系数据,则可以尝试:
whereHas
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.