manojvarma's avatar

Getting count from a array

HI, i am storing an array of values in each row of a database tables. Now i want to get the count of the array in each row and display it in tables. How can i do that?

$cust = DB::table('invoice')->join('customer', 'invoice.customer_id', '=', 'customer.id')->join('cities', 'customer.cities_id', '=', 'cities.id')->join('states', 'cities.state_id', '=', 'states.id')->join('districts', 'customer.districts_id', '=', 'districts.id')->select('invoice.*', 'customer.customer_name','customer.phone','customer.street','customer.locality','customer.PIN_code','customer.email','states.state','cities.city','districts.district')->get();

        return view('pages.viewinvoice', ['custom' => $cust]);

this is my controller code.

0 likes
5 replies
manojvarma's avatar

i have 50 rows in a database table. in each row in one column i am storing an array of values. Now i want to show the count of that array of each row in front end.

bobbybouwmann's avatar

Not really sure what you mean, but I think it's something like this

@foreach ($cust as $invoice)
    <p>Count: {{ count($invoice->array_field) }}</p>
@endforeach
1 like
manojvarma's avatar

@bobbybouwmann it is showing 1 to all the rows. actually what i mean is.

Collection {#486 ▼
  #items: array:64 [▼
    0 => "[107]"
    1 => "[108]"
    2 => "[109]"
    3 => "[11]"
    4 => "[125]"
    5 => "[127,133,129,140,126,124,280,216,262,215,217,214,65,132,137]"
    6 => "[128]"
    7 => "[130]"
    8 => "[138]"
    9 => "[144,152,171,172,168]"
    10 => "[146]"
    11 => "[146]"
    12 => "[151,147]"
    13 => "[17]"
    14 => "[180]"
    15 => "[183,211,416]"
    16 => "[205,196]"
    17 => "[207]"
    18 => "[25,89,79]"
    19 => "[288,287,291,292,290]"
    20 => "[29]"
    21 => "[303,318,311]"
    22 => "[304]"
    23 => "[304]"
    24 => "[304]"
    25 => "[304]"
    26 => "[304]"
    27 => "[304]"
    28 => "[304]"
    29 => "[304]"
    30 => "[304]"
    31 => "[305,307,316,310,312]"
    32 => "[308]"
    33 => "[309]"
    34 => "[315,324,411,416,418,429,447,448,451,454,480,485,503,527,528,529,531,532,534,536,537,538,539,540,542,543,544,642,645,646]"
    35 => "[319]"
    36 => "[323]"
    37 => "[325]"
    38 => "[328,332,334,330]"
    39 => "[337]"
    40 => "[338,339]"
    41 => "[340]"
    42 => "[368]"
    43 => "[377,379,314,306,380,317,378,367,381,369,366,382,384,383,387]"
    44 => "[410,412,385,386]"
    45 => "[413,533]"
    46 => "[414]"
    47 => "[415,417,455,452,450,430,432,446,445,453,428,431,456,457,427]"
    48 => "[419]"
    49 => "[481]"
    50 => "[484]"
    51 => "[486]"
    52 => "[494,322,498]"
    53 => "[499]"
    54 => "[5,19,31,15] "
    55 => "[502,131]"
    56 => "[530]"
    57 => "[67]"
    58 => "[7,1,37,9,13,23,21,33,39,27]"
    59 => "[71]"
    60 => "[73,75,77,83,106]"
    61 => "[762]"
    62 => "[85,3]"
    63 => "[87,420,421,482,483,488,497,526,535,541]"
  ]
}

i am getting the collection like this. and i want to show the count of each item in the view.

this is my controller code.

 $cust = DB::table('invoice')->join('customer', 'invoice.customer_id', '=', 'customer.id')->join('cities', 'customer.cities_id', '=', 'cities.id')->join('states', 'cities.state_id', '=', 'states.id')->join('districts', 'customer.districts_id', '=', 'districts.id')->select('invoice.*', 'customer.customer_name','customer.phone','customer.street','customer.locality','customer.PIN_code','customer.email','states.state','cities.city','districts.district')->get();

and this is my view

 @foreach($custom as $cust )
                        
                       <tr>
                        <td>{{ $cust->customer_name}}</td>
                        <td>{{ $cust->phone}}</td>
                        <td>{{ $cust->state}}</td>
                        <td>{{ $cust->district}}</td>
                        <td>{{ "$cust->city"}}</td>
                        
                        
                        
                        <td>@if($cust->mode_of_payment == 1)
                              Cash   
                              @elseif ($cust->mode_of_payment == 2)
                              NEFT
                              @elseif ($cust->mode_of_payment == 3)
                              Payumoney
                              @elseif ($cust->mode_of_payment == 4)
                              Amazon
                              @elseif ($cust->mode_of_payment == 5)
                              Ebay
                              @elseif ($cust->mode_of_payment == 6)
                              Cash on delivery
                              @elseif ($cust->mode_of_payment == 7)
                              Others
                              @endif </td>
                        <td><a>@if($cust->payment_status == 1)
                              <p1 style="color:red">NOT PAID</p1>
                              <a href="<?php echo 'payment/' .$cust-> id  ?>">Make it Paid</a>
                            @elseif ($cust->payment_status == 2)
                              <p1 style="color:green">RECEIVED<p1> 
                            @elseif ($cust->payment_status == 3)
                              <p1>NOT APPLICABLE<p1>  
                              @endif</a></td>       
                        <td>{{ $cust->salesperson}}</td> 

                        <td>{{($cust->device_id)}}</td>





                        <td>{{ $cust->invoice_number}}</td>
                        <td>{{ $cust->invoice_date}}</td>
                        {{--<td>{{ $cust->device_id}}</td>--}}
                        
                        {{--<td><a href="{{ url('invoice') }}">Show invoice</a></td>--}}
                        <td><a href="<?php echo 'invoice/' .$cust-> id  ?>">print invoice</a></td>
                        
                      
                        </tr>
                        @endforeach

in the view now i am showing array as it is " {{($cust->device_id)}}". instead of doing that. i need to show the count of device_id's.

bobbybouwmann's avatar

It's a string... You can't count the values of a string. What you can do is something like this, but I find this very ugly. You should not save data like this in your database.

$collection->each(function ($invoice) {
    $withoutBrackets = str_replace(array('[', ']'), '', $invoice);

    $arrayOfValues = explode(',', $withoutBrackets);
    
    return array_sum($array);
});

$collection->all();

// Returns something like this

Collection {#486 ▼ #items: array:64 [▼ 0 => "107" 1 => "108" 2 => "109" 3 => "11" 4 => "125" 5 => "2517"

Please or to participate in this conversation.