123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- namespace App\Exports;
- use App\Base\Utils;
- use App\Models\Order\Order;
- use App\Models\Order\OrderGoods;
- use App\Models\User\User;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Arr;
- use Maatwebsite\Excel\Concerns\FromCollection;
- class UserOrderExport implements FromCollection
- {
- public $params;
- /**
- * @param $params
- */
- public function __construct($params)
- {
- $this->params = $params;
- }
- public function collection()
- {
- $data = Order::where("user_id", $this->params['user_id'])->orderByDesc("id")->get()->map(function (Order $model) {
- $goodsInfo = "";
- foreach ($model->goods as $g) {
- $goodsInfo .= sprintf("%s %s x %s (%s) \n", $g->goods->name, $g->sn, $g->count, implode("-", Arr::pluck($g->custom ?? [], "value")));
- }
- return [
- "id" => $model->id,
- "goods" => $goodsInfo,
- "user_id" => Utils::idNo4($model->user_id),
- "name" => $model->user->name,
- "phone" => $model->user->phone,
- "email" => $model->user->email,
- "created_at" => $model->created_at->format("Y-m-d H:i:s"),
- ];
- });
- $data->prepend(["编号", "商品信息", "用户ID", "用户昵称", "用户手机", "用户邮箱", "询价时间"]);
- return $data;
- }
- }
|