php artisan make:export MemberOrdersExport --model=Models/Prod/Order
<?php
namespace App\Exports;
use App\Models\Prod\Order;
use Maatwebsite\Excel\Concerns\FromCollection;
class MemberOrdersExport implements FromCollection
{
protected $params = [];
public function __construct($params)
{
$this->params = $params;
}
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
//return Order::all();
return collect($this->getOrderItems());
}
public function getOrderItems(){
$user_id = $this->params['user_id'];
$chemical_info = $this->params['chemical_info'];
$order_no = $this->params['order_no'];
$status = $this->params['status'];
$pay = $this->params['pay'];
$send = $this->params['send'];
$start_time = $this->params['start_time'];
$end_time = $this->params['end_time'];
$query = Order::with(['user.user','item.prod','inquiry.invoices','prod'])->where('user_id','=',$user_id)->orderBy('id','desc');
if($start_time){
$query->where('created_at','>=',$start_time);
}
if($end_time){
$end_time = date('Y-m-d H:i:s',(strtotime($end_time)+86400));
$query->where('created_at','<',$end_time);
}
if($status != 0){
$query->where('status','=',$status);
}
if($pay != 2){
$query->where('is_pay','=',$pay);
}
if($send != 2){
$query->where('is_send','=',$send);
}
if($chemical_info){
$query->whereHas('prod',function($q) use ($chemical_info) {
$q->where('cas', 'like', '%'.$chemical_info.'%')->orWhere('name_cn', 'like', '%'.$chemical_info.'%')->orWhere('name', 'like', '%'.$chemical_info.'%');
});
}
if($order_no){
$query->where('order_no','=',$order_no);
}
$res = $query->get()->toArray();
$data = [];
$data[0] = ['订单号','下单时间','cas','产品名称','规格数量','金额','付款状态','发货状态','运单号'];
foreach ($res as $k=>$v){
$package = '';
foreach ($v['item'] as $k1=>$v1){
$package .= $v1['package'] . $v1['package_unit']."/";
}
$package = trim($package,'/');
$data[] = array(
'order_id'=>$v['order_no'],
'created_at'=>$v['created_at'],
'cas'=>$v['cas'],
'prod_name'=>$v['prod']['name_cn'],
'package'=>$package,
//'total_price'=>number_format($v['total_price'],2),
'total_price'=>$v['total_price'],
'is_pay'=>($v['is_pay']==1?'已付款':'未付款'),
'is_send'=>($v['is_send']==1?'已发货':'未发货'),
'express_num'=>$v['express_num'],
);
}
return $data;
}
}
<?php
namespace App\Http\Controllers\Home\Member;
use App\Exports\MemberOrdersExport;
use App\Library\Common;
use App\Library\Util;
public function export(Request $request){
$pay = $request->get('pay',2);
$send = $request->get('send',2);
$status = $request->get('status',0);
$order_no = $request->get('order_no');
$chemical_info = $request->get('chemical_info');
$start_time = $request->get('start_time');
$end_time = $request->get('end_time');
$user_id = isset(auth('member')->user()->id) ?auth('member')->user()->id : 0;
$params = array(
'pay'=>$pay,
'send'=>$send,
'status'=>$status,
'order_no'=>$order_no,
'chemical_info'=>$chemical_info,
'start_time'=>$start_time,
'end_time'=>$end_time,
'user_id'=>$user_id,
);
$export_name = 'orders_'.date('Y-m-d H:i:s',time()).".xlsx";
return Excel::download(new MemberOrdersExport($params),$export_name);
}
<script language="javascript">
$('#start_time').fdatepicker({
format: 'yyyy-mm-dd',
//pickTime: true
});
$('#end_time').fdatepicker({
format: 'yyyy-mm-dd',
//pickTime: true
});
$("#export_btn").click(function () {
if(confirm("确定导出订单吗?")){
var order_no = $("#order_no").val();
var chemical_info = $("#chemical_info").val();
var pay = $("#pay").val();
var send = $("#send").val();
var status = $("#status").val();
var start_time = $("#start_time").val();
var end_time = $("#end_time").val();
window.location.href = '/member/order/export?order_no='+order_no+"&chemical_info="+chemical_info+"&pay="+pay+"&send="+send+"&status="+status+"&start_time="+start_time+"&end_time="+end_time;
}
});
</script>
参考文档:https://docs.laravel-excel.com/3.1/exports/