0

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
DolDurma
  • 15,753
  • 51
  • 198
  • 377

2 Answers2

1

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

Humza Faqi
  • 201
  • 2
  • 11
1

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();
Santi Barbat
  • 2,097
  • 2
  • 21
  • 26
  • i get this error: `Syntax error or access violation: 1055 'xDATABASEx.users.id' isn't in GROUP BY (SQL: select `users`.*, `ticket_user`.`ticket_id` as `pivot_ticket_id`, `ticket_user`.`user_id` as `pivot_user_id` from `users` inner join `ticket_user` on `users`.`id` = `ticket_user`.`user_id` where `ticket_user`.`ticket_id` in (1, 2) and `users`.`deleted_at` is null group by `user_id`)` – DolDurma Jun 15 '20 at 11:43
  • [Check this one :)](https://stackoverflow.com/questions/40917189/laravel-syntax-error-or-access-violation-1055-error) – Santi Barbat Jun 15 '20 at 12:15