Remove multiple column duplicate value in Foreach loop

1.7k Views Asked by At

I use this code to display certain users per record in multiple property name. Though I wanted to display 1 name instead of multiple including their IDs.

@foreach( $records as $record )
  @if( $record->investment_type == null )
  <tr>
    <td>{{ $record->client_id }}</td>
    <td>{{ $record->firstname }} {{ $record->lastname }}</td>
    <td>{{ $record->name }}</td>
    <td class="details-control"></td>
  </tr>
  @endif
@endforeach

Here's the output of my current code.

enter image description here

While this is what i wanted to be looked like.

enter image description here

I tried, array_unique but the output gives me error

enter image description here

Source code came from this: php Removes duplicate values in foreach and just followed what was solved the issue from the poster. But it don't work on my end.

Many thanks for your help.

UPDATE:

Here's the output of the $records from var_dump.

enter image description here

And this is the code inside controller.

$records = DB::table('assets')
                ->join('client', 'client.Id', '=', 'assets.client_id')
                ->join('property', 'property.Id', '=', 'assets.property_id')
                ->select('assets.client_id','client.firstname','client.lastname','property.name','assets.investment_type')
                ->orderBy('assets.client_id')
                ->get();
2

There are 2 best solutions below

7
On

Not much familiar in laravel syntax. You can just convert it if you want. Anyway, to do this you need to check the previous value. I assume that this is sorted by name.

First create a previous variable

<?php $prev_value =  null;?>

Here is the code where we check if it is the same in the previous value

<?php if($prev_value == ($record->client_id)):?>
<td></td>
<td></td>
<?php else: ?>
<td>{{ $record->client_id }}</td>
<td>{{ $record->firstname }} {{ $record->lastname }}</td>
<?php endif; ?>

At the end of loop just add the current value in previous value variable

<?php $prev_value = $record->client_id; ?>

Whole Code

<?php $prev_value =  null;?>
<?php foreach( $records as $record ): ?>
  <?php if( $record->investment_type == null ): ?>
  <tr>

    <?php if($prev_value == ($record->client_id)):?>
    <td></td>
    <td></td>
    <?php else: ?>
    <td>{{ $record->client_id }}</td>
    <td>{{ $record->firstname }} {{ $record->lastname }}</td>
    <?php endif; ?>

    <td>{{ $record->name }}</td>
    <td class="details-control"></td>

    <?php $prev_value = $record->client_id; ?>

  </tr>
  <?php endif; ?>
<?php endforeach; ?>
0
On

You can’t use array_unique because you have an array of object. Create an array of Id and an array of name, In each loop you add the current id and the current name to theses arrays and before create the you check if the current datas are in the array, if not you add them to the if they already exist you create an empty .

Of course you have to check if Is in array from the second loop to display at minimums the first row. Please note that this method is dirty because you do some logical operation in the view. The best way is to do that on your model.