php - Unique visit per IP and dates
Get the solution ↓↓↓I have two tables: users and visits, I'm storing in visit tables how many users visited site (Offer model has many Visits) how to count unique visits by IP for all time? $offer->visits->groupBy('ip')->count() return number of views for IP of all time. I want to see all the unique visits for all days, eg.
User has IP 192.168.0.1 He visited one site:
25.04.2020 - He visited once.
26.04.2020 - He visited 3 times.
groupBy('ip')->count() - returns 1 view.
I want to return 2 views (because of unique one visit per date)
How to do that?
Answer
Solution:
You will need to groupBy today's date & IP for your relation. You can achieve that by using:
$offers = Offer::with(['visits' => function ($query) {
$query->select(DB::raw('DATE(created_at) as date'), 'ip','offer_id')
->groupBy('date', 'ip');
}])
->get();
foreach ($offers as $offer){
dump($offer->visits);
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: zsh: command not found: php
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.