Reputation: 150
I have some code, and I run it but it always includes a null array. How can I get only not null data?
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;
}
Returned 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}],[],[],[],[],[],[],[]]
How to not include the data in the end [],[],[]...
?
Upvotes: 0
Views: 538
Reputation: 396
Actualy you can do this very easly without any loop:
$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;
Upvotes: 1
Reputation: 86
foreach ($list_no_invoice as $key => $value) {
$check = Data::where('invoice_no', $value->invoice_no)->get();
if (sizeof($check) > 0) {
array_push($data, $check);
}
}
Upvotes: 1
Reputation: 2438
You can add check !empty()
to dont add empty array data
foreach ($list_no_invoice as $key => $value) {
$check = Data::where('invoice_no', $value->invoice_no)->get();
if (!empty($check)) {
array_push($data, $check);
}
}
Upvotes: 0