-1

When I trying' to get value from database using aliases "as", I got an error like this :

at HandleExceptions->handleError(8, 'Undefined offset: 0', '/data/gui/hum/hum.v3.1/vendor/laravel/framework/src/Illuminate/Support/Collection.php', 1570, array('key' => 0))

I tried to change the data stored in the temporary variable to array. I use method from laravel "collect()" to collect data in the form of objects.

This is my function to get data and return view for get data usage from database.

$dtusagearray = DB::select(DB::raw(" select    
            sum.msisdn msi,
            sum.imsi ims,
            sum.customer_status st,
            sum.grapari_name grp,
            substr(sum.customer_group,1,3) cg,
            sum.regional_name reg,
            sum.billperiod eop,
            sum.totalcharge tch,
            to_char(sum.totalcharge,'999,999,999,999') tc,
            to_char(sum.riskscore,'999,999,999,999') cr,
            to_char(sum.billingavg,'999,999,999,999') avb,
            to_char(sum.billingmax,'999,999,999,999') mab,
            to_number(to_char(sum.lastcall,'DD')) day,
            to_char(ceil(sum.billingavg/(ceil(to_number(to_char(sum.lastcall,'DD'))))),'999,999,999,999') avchday,
            to_char(sum.lastcall,'dd/mm/yyyy hh24:mm:ss') lc,
            (select count(msisdn) from actions a where a.msisdn=sum.msisdn and a.billperiod=sum.billperiod and a.actiontype='5') jml_call,
            (select count(msisdn) from actions a where a.msisdn=sum.msisdn and a.billperiod=sum.billperiod and a.actiontype='3') jml_sms,
            (select count(msisdn) from actions a where a.msisdn=sum.msisdn and a.billperiod=sum.billperiod and a.actiontype='2') jml_adj,
            (select count(msisdn) from actions a where a.msisdn=sum.msisdn and a.billperiod=sum.billperiod and a.actiontype='1') jml_blk,
            nvl(sum.roam_name, sum.lastroam) lr
        from master_ip333_charge sum
        where sum.msisdn = '62'||'$msisdn'
        order by sum.billperiod asc 

And this is how I trying' to convert data from object and return view for show the result

$dtusagearray = collect($dtusagearray);
$dtusage = $dtusagearray[0];
return view('customerprofile', compact('dtusage'));

The output of this function is to display customer usage data, based on the results of the calculation of the queries.

elkecp
  • 21
  • 1
  • 1
  • 4
  • What is the `[0]` supposed to be doing? Getting the first item? I think the collection has a method for that – Don't Panic Apr 18 '19 at 16:35
  • @Don'tPanic It does; you can do `collect(...)->first()`, which will return the first value or `null` if not set. `collect(...)[0]` will still work, but you need to manually check via `isset()`. That being said, `$dtusagearray` is missing a closure, like `->get()` (which converts to a `Collection`) or `->first()`, which bypasses the `Collection` entirely and just returns the first record (or `null`). – Tim Lewis Apr 18 '19 at 16:37
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 Apr 18 '19 at 16:38
  • Yes, correct. But, when I trying' method first(), I got an error again and I can't use the method first() because the data taken is not only on the first line. all lines will be displayed – elkecp Apr 18 '19 at 16:42

1 Answers1

0

You can convert your data to array and see if you can access the offset 0.

Your code will be like :

$dtusagearray = (array) $dtusagearray;
$dtusage = $dtusagearray[0];
return view('customerprofile', compact('dtusage'));