Laravel 基于其他表的雄辩where子句
我的一个Laravel模型叫做Property,它包含我的应用程序中的所有不动产属性。每个属性包含一个城市和一个国家 因此,我有以下关系表:Laravel 基于其他表的雄辩where子句,laravel,laravel-5,eloquent,Laravel,Laravel 5,Eloquent,我的一个Laravel模型叫做Property,它包含我的应用程序中的所有不动产属性。每个属性包含一个城市和一个国家 因此,我有以下关系表: City_property (holds city of properties) | id | city_id | property_id | |----|---------|-------------| | x | 2 | 1 | City_country (holds country of city) | id
City_property (holds city of properties)
| id | city_id | property_id |
|----|---------|-------------|
| x | 2 | 1 |
City_country (holds country of city)
| id | country_id | city_id |
|----|------------|---------|
| x | 3 | 2 |
因此,在获取我的所有属性时,我希望在属性的国家/地区上使用where子句,因此我只能获取属性where Country=5,例如。您可以结合where方法使用。例如:
Property::join('City_property', 'properties.id', '=', 'City_property.id')
->join('City_country', 'City_property.city_id' , '=', 'City_country.city_id')
->where('City_country.country_id', 5)
->get();
您可以使用查询生成器whereHas方法,该方法对关系执行查询。在本例中,我将您的模型重命名为Property and Country。这也假定存在国家关系
Property::whereHas('country', function ($query) {
$query->where('id', 5);
})->get();
通常情况下,在模型文件中使用Laravel关系很容易,但我使用的是一个创建这些数据库文件的小型CMS。请问您使用的是哪种CMS?@Thamerbelfkih Twillcms但您仍然可以使用关系?“在那个结构中没有任何东西是阻挡的。”MartinHenriksen怎么说?