Browse Source

1、首页广告图改成专题;2、关联查询指定字段代码简化;

dev
李可松 4 years ago
parent
commit
cdf6a2fe23
  1. 17
      app/Http/Controllers/Api/IndexController.php

17
app/Http/Controllers/Api/IndexController.php

@ -2,11 +2,11 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Advertising;
use App\Models\Channel; use App\Models\Channel;
use App\Models\Notice; use App\Models\Notice;
use App\Models\AgentProduct; use App\Models\AgentProduct;
use App\Models\Slide; use App\Models\Slide;
use App\Models\Special;
use App\Models\UserChannel; use App\Models\UserChannel;
/** /**
@ -28,23 +28,26 @@ class IndexController extends Controller
# 我的频道 # 我的频道
$channel_ids = UserChannel::where('user_id', $this->user_id)->value('channels'); $channel_ids = UserChannel::where('user_id', $this->user_id)->value('channels');
$my_channels = Channel::whereIn('id', $channel_ids)->get(['id', 'pid', 'name', 'icon']);
$my_channels = Channel::whereIn('id', $channel_ids)->get(['id', 'name', 'icon']);
# 广告图
$home_ad1 = Advertising::where('agent_id', $this->agent_id)->value('picture');
# 专题列表
$special = Special::query()
->where('agent_id', $this->agent_id)
->orderBy('sort')
->orderBy('id')
->get(['id', 'picture_ad']);
# 人气爆款 # 人气爆款
// $hots = AgentProduct::select(['title', 'price', 'original_price', 'pictures'])->get();
$hots = AgentProduct::query() $hots = AgentProduct::query()
->select('id', 'sale', 'product_id', 'price', 'original_price') //必须查询product_id才能with ->select('id', 'sale', 'product_id', 'price', 'original_price') //必须查询product_id才能with
->with(['product' => fn ($query) => $query->select('id', 'title', 'pictures')]) //必须查询ID才能正常查询
->with('product:id,title,pictures') //必须查询ID才能正常查询
->limit(6)->get(); ->limit(6)->get();
return $this->success([ return $this->success([
'slide' => $slide, 'slide' => $slide,
'notice' => $notice, 'notice' => $notice,
'my_channels' => $my_channels, 'my_channels' => $my_channels,
'home_ad1' => $home_ad1,
'special' => $special,
'hots' => $hots, 'hots' => $hots,
]); ]);
} }

Loading…
Cancel
Save