Following should work:
@foreach ($users as $user)
{{ $user->pluck('ip')->unique()->count() }}
@endforeach
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi everyone,
I have a question I can't sort out. I have a table where I register every time a user logs in my website. In this table I record user id, ip, date in a table a have called "customer_logs". I have my Model CustomerLog related with the model Customer:
class CustomerLog extends Model
{
protected $table = 'customers_log';
protected $fillable=[
'id',
'customer_id',
'ip',
'created_at',
];
public function customer()
{
return $this->belongsTo('App\Customer');
}
This is an example of the records in this table:
user_1 - 192.0.0.1 - 2021-11-23
user_1 - 192.0.0.2 - 2021-11-23
user_1 - 192.0.0.3 - 2021-11-23
user_2 - 192.1.1.1 - 2021-11-23
I want to get a list of the customers (user_1 and user_2 in this example) and I want to show how many IPs the user has used and how many records has the user un te table. This is an example:
user_1_name - 3ips - 3 rows
user_2_name - 1ips - 1 rows
I have this elquence sentence, I group by customer but after that, what would be the best way to get the data I want to show?
$users=CustomerLog::whereDate('created_at', Carbon::today())->groupBy('customer_id')->get();
Thanks for your answers
I think you should look at using raw expressions in your query. In fact, the example given in the documentation is getting a user count after a group by.
Please or to participate in this conversation.