2016-11-01 24 views
0

我有我的Laravel項目中的以下車型優化,以獲得所需的Laravel查詢數據

  1. 商人
  2. 優惠
  3. 新政投票
  4. 新政點擊次數
  5. 交易交易類型
  6. 商人評分

這是我的招商模式看起來像

class Merchants extends Model 
{ 
    // 
    public function deals() 
    { 
     return $this->hasMany('App\Deals', 'merchant_id', 'merchant_id'); 
    } 
} 

優惠型號

class Deals extends Model 
{ 
// 
    public function clicks() 
    { 
     return $this->hasMany('App\Clicks', 'deal_id', 'deal_id'); 
    } 
    public function merchants() 
    { 
     return $this->hasOne('App\Merchants', 'deal_id', 'deal_id'); 
    } 
    public function votes() 
    { 
     return $this->hasMany('App\Deal_votes', 'deal_id', 'deal_id'); 
    } 
    public function deal_dealtypes() 
    { 
     return $this->hasMany('App\Deal_dealtypes', 'deal_id', 'deal_id'); 
    } 
} 

商家評級模型的

public function merchants() 
    { 
     return $this->belongsTo('App\Merchants', 'merchant_id', 'merchant_id'); 
    } 

對於特定商家着陸頁,我試圖得到一些商家信息,與商家相關的交易,每筆交易的投票和點擊次數。這是我迄今

$merchant = Merchants::where('merchant_url_text', $merchant_url_text) 
       -> with('categories' ,'deals','deals.votes','deals.clicks', 'deals.deal_dealtypes') 
       -> first(); 

然而,這是返回商戶信息,交易信息,所有的選票記錄綁在新政和所有的點擊綁每筆交易完成。

我越來越

{ 
 
merchant_id: 6605, 
 
master_merchant_id: null, 
 
skimlinks_id: null, 
 
merchant_name: "Groupon", 
 
network: "cj", 
 
network_id: "", 
 
network_notes: null, 
 
dual_network: null, 
 
related_merchants: null, 
 
affiliate_link: "http://api.coupilia.com/merchants/?aid=1423&mid=6605", 
 
skimlinks_url: null, 
 
fmtcurl: null, 
 
merchant_homepage_url: "www.groupon.com?z=dealpage", 
 
status: null, 
 
selected_status: null, 
 
relationship_status: null, 
 
primary_country: "US", 
 
apofpo: null, 
 
primary_category: null, 
 
subcategories: null, 
 
special_payment_options: null, 
 
mobile_certified: null, 
 
logos: "", 
 
custom_logo: "https://res-5.cloudinary.com/dhhntbusx/image/upload/t_media_lib_thumb/v1475206550/Groupon-coupons_mzjdm8.jpg", 
 
custom_description: null, 
 
added: null, 
 
lastupdated: null, 
 
merchant_url_text: "groupon.com", 
 
about: "", 
 
primary_category_id: "43", 
 
parent_merchant_id: null, 
 
skimlinks_ids: null, 
 
custom_merchant_name: null, 
 
ship_to_countries: "", 
 
created_at: "2016-10-07 08:04:12", 
 
updated_at: "2016-10-07 08:04:12", 
 
categories: { 
 
id: 43, 
 
slug: "social", 
 
category_name: "Social", 
 
category_url_name: "social", 
 
category_parent_id: null, 
 
category_grandparent_id: null, 
 
created_at: "2016-10-07 08:04:03", 
 
updated_at: "2016-10-07 08:04:03" 
 
}, 
 
deals: [ 
 
{ 
 
deal_id: 875438, 
 
merchant_name: "Groupon", 
 
merchant_id: 6605, 
 
status: null, 
 
deal_text: "Save Up To 90% Off Local Restaurants, Shops, Events And More", 
 
restrictions: "", 
 
coupon_code: "", 
 
deal_start_date: "1/31/2016", 
 
deal_end_date: "11/6/2016", 
 
discount_type: null, 
 
deal_type: "deal", 
 
affiliate_url: "", 
 
final_sale_price: null, 
 
was_price: null, 
 
discount: null, 
 
percent_off: null, 
 
dollars_off: null, 
 
network: null, 
 
image: null, 
 
skimlinks_url: null, 
 
fmtcurl: null, 
 
created: null, 
 
lastupdated: null, 
 
threshold: null, 
 
rank: "3", 
 
starred: null, 
 
master_merchant_id: null, 
 
brands: null, 
 
local: null, 
 
link_id: null, 
 
direct_url: null, 
 
pixel_html: null, 
 
created_at: "2016-10-07 08:05:26", 
 
updated_at: "2016-10-07 08:05:26", 
 
votes: [ 
 
{ 
 
id: 2, 
 
deal_id: 875438, 
 
vote: 1, 
 
user_id: 0, 
 
uuid: "ad63d5a0-2582-11e6-9a1c-49da886e8d6c", 
 
created_at: "2016-10-12 05:23:57", 
 
updated_at: "2016-10-12 05:23:57" 
 
}, 
 
{ 
 
id: 5, 
 
deal_id: 875438, 
 
vote: 1, 
 
user_id: 0, 
 
uuid: "bde41ea0-903c-11e6-b93b-51bc5393a427", 
 
created_at: "2016-10-12 05:28:50", 
 
updated_at: "2016-10-12 05:28:50" 
 
} 
 
], 
 
clicks: [ 
 
{ 
 
id: 1, 
 
tracking_id: null, 
 
merchant_id: null, 
 
deal_id: "875438", 
 
user_id: null, 
 
user_tracking_id: null, 
 
referring_url: "http://trystin.app:8000/deals-coupons/groupon.com", 
 
referring_domain: null, 
 
page_type: null, 
 
page_url: null, 
 
utm_source: null, 
 
utm_medium: null, 
 
utm_term: null, 
 
utm_campaign_id: null, 
 
os: "OS X", 
 
ip_address: "10.0.2.2", 
 
browser: "Chrome", 
 
initial_referring_domain: null, 
 
referring_keyword: null, 
 
deal_position: null, 
 
click_type: null, 
 
created_at: "2016-10-12 02:19:59", 
 
updated_at: "2016-10-12 02:19:59" 
 
}, 
 
{ 
 
id: 12, 
 
tracking_id: null, 
 
merchant_id: null, 
 
deal_id: "875438", 
 
user_id: null, 
 
user_tracking_id: null, 
 
referring_url: "http://trystin.app:8000/deals-coupons/groupon.com", 
 
referring_domain: null, 
 
page_type: null, 
 
page_url: null, 
 
utm_source: null, 
 
utm_medium: null, 
 
utm_term: null, 
 
utm_campaign_id: null, 
 
os: "OS X", 
 
ip_address: "10.0.2.2", 
 
browser: "Chrome", 
 
initial_referring_domain: null, 
 
referring_keyword: null, 
 
deal_position: null, 
 
click_type: null, 
 
created_at: "2016-10-21 03:41:11", 
 
updated_at: "2016-10-21 03:41:11" 
 
}, 
 
{ 
 
id: 15, 
 
tracking_id: null, 
 
merchant_id: null, 
 
deal_id: "875438", 
 
user_id: null, 
 
user_tracking_id: null, 
 
referring_url: "http://trystin.app:8000/deals-coupons/groupon.com", 
 
referring_domain: null, 
 
page_type: null, 
 
page_url: null, 
 
utm_source: null, 
 
utm_medium: null, 
 
utm_term: null, 
 
utm_campaign_id: null, 
 
os: "OS X", 
 
ip_address: "10.0.2.2", 
 
browser: "Chrome", 
 
initial_referring_domain: null, 
 
referring_keyword: null, 
 
deal_position: null, 
 
click_type: null, 
 
created_at: "2016-10-21 03:55:33", 
 
updated_at: "2016-10-21 03:55:33" 
 
}, 
 
{ 
 
id: 16, 
 
tracking_id: null, 
 
merchant_id: null, 
 
deal_id: "875438", 
 
user_id: null, 
 
user_tracking_id: null, 
 
referring_url: "http://trystin.app:8000/deals-coupons/groupon.com", 
 
referring_domain: null, 
 
page_type: null, 
 
page_url: null, 
 
utm_source: null, 
 
utm_medium: null, 
 
utm_term: null, 
 
utm_campaign_id: null, 
 
os: "OS X", 
 
ip_address: "10.0.2.2", 
 
browser: "Chrome", 
 
initial_referring_domain: null, 
 
referring_keyword: null, 
 
deal_position: null, 
 
click_type: null, 
 
created_at: "2016-10-21 04:06:08", 
 
updated_at: "2016-10-21 04:06:08" 
 
} 
 
], 
 
deal_dealtypes: [ 
 
{ 
 
id: 1888, 
 
deal_id: "875438", 
 
dealtype_id: "3", 
 
created_at: "2016-10-07 08:05:26", 
 
updated_at: "2016-10-07 08:05:26" 
 
} 
 
] 
 
}, 
 
}

附樣本數據的反應,我需要一些幫助,下面

  1. 獲取有限的領域,不是所有領域依賴於商家
  2. 從交易對象
  3. 獲取有限的字段
  4. 在響應

回答

0

表決,且每筆交易次數的獲取計數爲了讓有限的領域,你可以使用select()功能,並獲得數可以使用withCount()功能。

所以您的查詢就會爲:

$merchant = Merchants::where('merchant_url_text', $merchant_url_text) 
        ->select('id', 'name') 
        ->with(['deals' => function($q) { 
          $q->select('id', 'name') 
           ->with('votes','clicks', 'deal_dealtypes') 
           ->withCount('votes') 
           ->withCount('clicks'); 
         }]) 
        ->with('categories') 
        -> first(); 
+0

我得到一個錯誤信息說解析錯誤:語法錯誤,意外的「}」。不知道爲什麼 –

+0

使用代碼,但現在我得到以下回應。 { merchant_id:6605, 交易:[] }交易是空的。 –

+0

可能有一個機會,沒有在DB中爲merchant_id = 6605輸入交易。 –