1

I need to display the previous value of the iterated array, here is code

<tr>
    <th></th>
    <th>Customer ID</th>
    <th>Customer Name</th>
    <th>Delivery Address</th>
    <th>Contact No.</th>
    <th>Zip Code</th>
    <th>Payment Terms</th>
    <th>Credit Limit</th>
</tr>
<?php $previousValue = null; ?>
@foreach($customer_count as $key => $value)
    <tr>
        <td>{{$previousValue=$value->customer_name }}</td>
        <td>{{ $value->id }}</td>
        <td>{{ $value->customer_name }}</td>
        <td>{{ $value->delivery_address }}</td>
        <td>{{ $value->contact_number }}</td>
        <td>{{ $value->area_id }}</td>
        <td>{{ $value->payment_term_id }}</td>
        <td>{{ $value->credit_limit }}</td>
    </tr>
@endforeach

I need to display the previous name of Customer in the iteration? Any idea on how to do this?

4 Answers 4

2

Set value at the end , I set blank for first

 @foreach($customer_count as $key => $value)
      <tr>
           <td>{{$previousValue or ""}}</td>
           .....
           .....
      </tr>
      <?php $previousValue = $value->customer_name ?>
  @endforeach
Sign up to request clarification or add additional context in comments.

1 Comment

This work thanks mate!!! specially the "or" part is so helpful !!! cheers
0

$customer_count[$key - 1]

Of course you'll need to check if that exists or use the null-coalesce operator or something, so:

@php

$previous = $customer_count[$key - 1] ?? false;

@endphp

Then use:

@if( $previous ) 
... some stuff ...
@endif

wherever you need to inject.

2 Comments

already tried this one but Undefined offset: -1 (View: C:\xampp\htdocs\fdis-laravel\resources\views\report\customer-count-group-detail.blade.php)
Yes, that why I said use the null-coalesce operator. Look at my edited answer.
0

please try this

<td>{{ (!$loop->first) ? $customer_count[$key - 1]->customer_name : '' }}</td>

You can read more about foreach loop here.

Comments

0

At very first set the previous value as blank and then at the bottom set value in $previousValue

<?php $previousValue = '';?>
@foreach($customer_count as $key => $value)
    <tr>
        <td>{{ $previousValue }}</td>
        <td>{{ $value->id }}</td>
        <td>{{ $value->customer_name }}</td>
        <td>{{ $value->delivery_address }}</td>
        <td>{{ $value->contact_number }}</td>
        <td>{{ $value->area_id }}</td>
        <td>{{ $value->payment_term_id }}</td>
        <td>{{ $value->credit_limit }}</td>
    </tr>
    <?php $previousValue = $value->customer_name; ?>
@endforeach

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.