Newbie
Newbie

Reputation: 47

How to show unique data on my table using php?

Using this code:

<?php $contacts = Group::where('account_id','=', Auth::user()->account->account_id)->get(); ?>
      @if ( count( $data['contacts'] ) > 0 )
        @foreach( $data['contacts'] as $contact )
        <tr>
        <?php $ucontact = \App\User::find($contact->user_id)?>
            <td>{{$ucontact->group->account_name}}</td>
            <td>{{$contact->event_name}}</td>
        @endforeach
      @else

Helps me show the data on my table. The problem is it shows duplicate datas. Is it possible to show only the unique ones?
Like for example:

Account Name Event Name
Account 1   Event 1
Account 2   Event 2
Account 1   Event 3

Account 1 is shown twice. I need to hide the duplicating data on my table.

Upvotes: 1

Views: 61

Answers (3)

Shrikant Systematix
Shrikant Systematix

Reputation: 21

You need to add group by clause in your query.

Here is the sample code.

<?php
$contacts = Group::where('account_id', Auth::user()->account->account_id)->groupBy('account_id')->get();
?> 

Upvotes: 0

saravanan
saravanan

Reputation: 145

You can try like this:

$contacts->unique( array('account_name','event_name') );

Upvotes: 0

Vineesh
Vineesh

Reputation: 467

Use group by account_id:-

Group::where('account_id','=', Auth::user()->account->account_id)->groupBy('account_id')->get()

Upvotes: 1

Related Questions