使用口才关系来检索子关系的数据

时间:2018-12-31 17:29:43

标签: laravel laravel-5 eloquent

我正在使用Laravel 5.6和MySQL。我有from pyzillow.pyzillow import ZillowWrapper, GetDeepSearchResults, GetUpdatedPropertyDetails import pandas as pd import numpy as np key = "X1-ZWz1gtmiat11xn_7ew1d" # Create function to get zillow_id def get_zillow_id(key, address, zipcode): zillow_data = ZillowWrapper(key) deep_search_response = zillow_data.get_deep_search_results(address, zipcode) result = GetDeepSearchResults(deep_search_response) return result.zillow_id # Create function to get propery data def get_property_data(key, address, zipcode): zillow_data = ZillowWrapper(key) updated_property_details_response = zillow_data.get_updated_property_details(get_zillow_id(key, address, zipcode)) result = GetUpdatedPropertyDetails(updated_property_details_response) return result.year_built # Import data into dataframe df = pd.read_csv('test.csv') # Create a new column to get year built df['Year Built'] = df.apply(lambda row: get_property_data(key, row['Address'], row['Zipcode']), axis=1) address = "5621 N Atlantic Ave, Portland, OR" zipcode = "97217" zillow_id = get_zillow_id(key, address, zipcode) zillow_data = ZillowWrapper(key) updated_property_details_response = zillow_data.get_updated_property_details(zillow_id) result = GetUpdatedPropertyDetails(updated_property_details_response) result.rooms # number of rooms of the home categoriessub_categoriescategory_info

一个services可以有一个或多个categorysub_categories属于一个菜单类别

一个category只有一个sub_category

一个category拥有1条category信息记录。不论是category还是category,它都将在category_info表中具有一条记录。

我的表结构如下

Menu_Categories

sub_category

类别

id
1,
2

Category_Info

id, parent_category_id, menu_category_id 
1, null, 1
2, null, 1
3, 1, null
4, 1, null

控制器

category_id, name 
1, Legal Services 
2, Misc 
3, US Legal Services 
4, Europe Legal Services 

菜单类别模型

$cats = MenuCategories
        ::with(['categories', 'categories.info', 'categories.sub_categories'])
        ->get();

类别模型

public function categories() {
    return $this->hasMany(Categories::class, 'menu_category_id', 'id');
}

子类别信息模型

public function menu_categories() {
    return $this->belongsTo(MenuCategories::class, 'menu_category_id');
}

   public function sub_categories() {
    return $this->hasOne(Categories::class, 'parent_category_id');
}

public function info() {
    return $this->hasOne(CategoryInfo::class, 'category_id');
}

我需要按如下所示返回类别和类别信息

    public function category() {
    return $this->belongsTo(CategoryInfo::class);
}

以我建立关系的方式,它错误地返回了数据。它没有为{ Categories: { { id: 1, parent_category_id, null, Sub_Categories: { { id: 3 parent_category_id: 1 Category_Info: { { category_id: 3, name: US Legal Services } } }, { id: 4 parent_category_id: 1, Category_Info: { { category_id: 4, name: Europe Legal Services } } }, }, Category_Info: { { category_id: 3, name: Legal Services } } }, { id: 2, parent_category_id, null, Category_Info: { { category_id: 3, name: Misc } } }, } } 返回Category_Info。

sub_categories

是否也可以为{ Categories: { { id: 1, parent_category_id, null, Sub_Categories: { { id: 3 parent_category_id: 1 }, { id: 4 parent_category_id: 1, }, }, Category_Info: { { category_id: 3, name: Legal Services } } }, { id: 2, parent_category_id, null, Category_Info: { { category_id: 3, name: Misc } } }, } } 返回Category_Info?预先感谢。

1 个答案:

答案 0 :(得分:0)

谢谢Jonas Staudenmeir帮助我解决问题

$cats = MenuCategories ::with(['categories', 'categories.info', 'categories.sub_categories', 'categories.sub_categories.info']) ->get();