DolDurma
DolDurma

Reputation: 17331

Laravel using unique for collections in relationships

getting data from database by this code:

$allTickets = Ticket::with(['user','replies','supporters'])->whereUserId(auth()->user()->id)->get();

return this result for me which that have 2 index in array and all of this items i have supporters relationship which both are same, now my question is how can i use unique in collections

Illuminate\Database\Eloquent\Collection {#1550 ▼
  #items: array:2 [▼
    0 => Modules\Ticket\Entities\Ticket {#1548 ▼
      #guarded: array:1 [▶]
      #connection: "mysql"
      #table: "tickets"
      ...
      #relations: array:3 [▼
        "user" => App\User {#1575 ▶}
        "replies" => Illuminate\Database\Eloquent\Collection {#1565 ▶}
        "supporters" => Illuminate\Database\Eloquent\Collection {#1572 ▼
          #items: array:1 [▼
            0 => App\User {#1585 ▼
              ...
              #attributes: array:19 [▼
                "id" => 1
                "user_id" => null
                ...
                "api_token" => "a"
                "remember_token" => null
                "deleted_at" => null
                "created_at" => "2020-06-14 13:56:45"
                "updated_at" => "2020-06-14 13:56:45"
              ]
              ...
            }
          ]
        }
      ]
      ...
    }
    1 => Modules\Ticket\Entities\Ticket {#1567 ▼
      #guarded: array:1 [▶]
      #connection: "mysql"
      #table: "tickets"
      ...
      #relations: array:3 [▼
        "user" => App\User {#1575 ▶}
        "replies" => Illuminate\Database\Eloquent\Collection {#1551 ▶}
        "supporters" => Illuminate\Database\Eloquent\Collection {#1559 ▼
          #items: array:1 [▼
            0 => App\User {#1585 ▼
              ...
              #attributes: array:19 [▼
                "id" => 1
                "user_id" => null
                ...
                "api_token" => "a"
                "remember_token" => null
                "deleted_at" => null
                "created_at" => "2020-06-14 13:56:45"
                "updated_at" => "2020-06-14 13:56:45"
              ]
              ...
            }
          ]
        }
      ]
      ...
    }
  ]
}

my tested code:

@foreach($allTickets as $supporter)
    @php
    $unique = $supporter->supporters->unique();
    @endphp
    @foreach($unique->values()->all() as $user)
      ...
      ...
      ...
    @endforeach
@endforeach

Upvotes: 0

Views: 213

Answers (2)

Humza Faqi
Humza Faqi

Reputation: 241

Or you can query like this:

$allTickets = collect(Ticket::with(['user','replies','supporters'])->whereUserId(auth()->user()->id)->get());
$allUniqueTickets = $allTickets->unique();
$tickets = $allUniqueTickets->values()->all();

That's how you can easily get all the unique values from your controller. I hope this answers your question. For more information, you can check https://laravel.com/docs/7.x/collections#method-unique

Upvotes: 2

Santi Barbat
Santi Barbat

Reputation: 2315

Do not try to do unique on the collection, try to get your data already filtered from your database. You could group by any of your relationships attributes, just pass in a closure to handle the query for the eager-loaded content, something like:

$allTickets = Ticket::with(['user','replies'])->with([
    'supporters' => function($query) {
        $query->groupBy('id');
    }
])->whereUserId(auth()->user()->id)->get();

Upvotes: 1

Related Questions