type = $type; $this->data = $this->createData(); } /** * @return \Illuminate\Support\Collection */ public function collection() { return new Collection($this->data); } private function createData() { $headTitle = [ '商品编码', '商品名称', '商品种类', '商品品牌', '规格编码', '规格名称', '成本', '库存', ]; $inventoryTime = strtotime(date('Y-m-d 07:00:00')); $ids = Log::query()->where('target_type', 'goods_sku') ->where('target_field', $this->type) ->where('created_at', '>', $inventoryTime) ->pluck('target_id') ->toArray(); $data = GoodsSku::query() ->when($ids, function ($query, $ids) { return $query->whereIn('id', $ids); }) ->with(['goods' => function ($query) { $query->with(['type:id,name', 'brand:id,name']); }]) ->get() ->toArray(); if (empty($data)) { return [$headTitle]; } $bodyData = []; foreach ($data as $item) { $arr[0] = $item['goods']['goods_code']; $arr[1] = $item['goods']['title']; $arr[2] = $item['goods']['type']['name']; $arr[3] = $item['goods']['brand']['name']; $arr[4] = $item['sku_code']; $arr[5] = $item['title']; $arr[6] = $item['cost']; $arr[7] = $item['stock']; $bodyData[] = $arr; } unset($arr); return [$headTitle, $bodyData]; } }