我有一些代码,可以运行它,但它始终包含一个空数组。如何仅获取非空数据?
public function index(Request $request)
{
$list_no_invoice = Invoice::whereNotNull('invoice_no')->get();
$data = array();
foreach ($list_no_invoice as $key => $value) {
$check = Data::where('invoice_no', $value->invoice_no)->get();
array_push($data, $check);
}
return $data;
}
返回的数据
[[{"id":2,"bulan":"januari","tahun":"2018","nama_pkp":"Achmad Rivai","keterangan":"DP 50% Sewa periode 15 Feb - 13 Mar'16 ( Pameran Nands )","no_faktur_pajak":"010.000-16.43061871","tgl_faktur_pajak":"2016-03-18","ref":"BPnB No.154\\/III\\/2016","dpp":10227273,"ppn":1022727,"invoice_no":"IR16020115","nama_tenant":"Nand;s","pph_4_deyon":1022727,"pph_4_tenant":null,"bukpot_oleh_tenant":null,"no_p_l_tenant":null,"tgl_p_l_penyewa":"1970-01-01","pph_p_l_tenant":null,"note":null,"checklist":1022727,"selisih":"0.30","created_at":null,"updated_at":null,"deleted_at":null,"pph_23":null,"has_edit":0}],[{"id":3,"bulan":"januari","tahun":"2018","nama_pkp":"Achmad Rivai","keterangan":"Pelunasana Sewa periode 15 Feb - 13 Mar'16 ( Pameran Nands )","no_faktur_pajak":"010.000-16.43061872","tgl_faktur_pajak":"2016-03-18","ref":"BPnB No.154\\/III\\/2016","dpp":10227273,"ppn":1022727,"invoice_no":"IR16020116","nama_tenant":"Nand;s","pph_4_deyon":1022727,"pph_4_tenant":null,"bukpot_oleh_tenant":null,"no_p_l_tenant":null,"tgl_p_l_penyewa":"1970-01-01","pph_p_l_tenant":null,"note":null,"checklist":1022727,"selisih":"0.30","created_at":null,"updated_at":null,"deleted_at":null,"pph_23":null,"has_edit":0}],[{"id":4,"bulan":"januari","tahun":"2018","nama_pkp":"Achmad Rivai","keterangan":"DP 25% Sewa periode 22 Feb - 27 Mar'16 (Pameran Nands Purple)","no_faktur_pajak":"010.031-16.63924514","tgl_faktur_pajak":"2016-04-15","ref":"BPnB No.169\\/IV\\/2016","dpp":5017727,"ppn":501773,"invoice_no":"IR16020047","nama_tenant":"Nand's Purple","pph_4_deyon":501773,"pph_4_tenant":null,"bukpot_oleh_tenant":null,"no_p_l_tenant":null,"tgl_p_l_penyewa":"1970-01-01","pph_p_l_tenant":null,"note":null,"checklist":501773,"selisih":"-0.30","created_at":null,"updated_at":null,"deleted_at":null,"pph_23":null,"has_edit":0}],[],[],[],[],[],[],[]]
如何不在结尾[],[],[]...
中包括数据?
答案 0 :(得分:1)
foreach ($list_no_invoice as $key => $value) {
$check = Data::where('invoice_no', $value->invoice_no)->get();
if (sizeof($check) > 0) {
array_push($data, $check);
}
}
答案 1 :(得分:1)
实际上,您可以非常轻松地执行此操作,而无需循环:
$list_no_invoice = Invoice::whereNotNull('invoice_no')->pluck('invoice_no')->toArray();
//get all records where invoice_no is one of values from array $list_no_invoice
$data = Data::whereIn('invoice_no',$list_no_invoice)->get()->toArray();
return $data;
答案 2 :(得分:0)
您可以添加支票!empty()
而不添加空数组数据
foreach ($list_no_invoice as $key => $value) {
$check = Data::where('invoice_no', $value->invoice_no)->get();
if (!empty($check)) {
array_push($data, $check);
}
}
答案 3 :(得分:-1)
在添加添加检查desc