I'm building minor a page monitoring tool, and I want to log clicks on a given page.
It's important that I later can query out clicks on a given day on a specific page etc. So far this is what I build, but I'm not sure if it's the best solution.
$clicks = Click::where('page_id', $pid)
->whereDate('created_at', DB::raw('CURDATE()'))
->get();
if($clicks->isEmpty()) {
$clicks = new click;
$clicks->clicks = 1;
$clicks->page_id = $pid;
$clicks->save();
}
else {
$clicks->first()->increment('clicks');
}
And when I want to show it to the user I'll be doing as follow:
@foreach ($page->click as $click)
<?php
if(check_in_range($from, $to, $click->created_at->format('Y-m-d'))) {
$clicks = $clicks + $click->clicks;
}
?>
@endforeach
{{ $clicks }}
So basically everytime someone visits the page, I'll create a new row in the database, but if there already is a click for that day I'll increment it.
Another solution could be just logging each click as one row, but that would very quickly be a lot of rows.
I'm looking for suggestions and a better solution (if any) :)
Maybe there are a lot of solutions. In this case maybe is convenient save all information about click in by row.
And only ask for the click in and specific day
$start_date = 'your date';
$end_date = 'your_date'; //Maybe you can give the choice to select a range of dates;
$clicks = Click::where('page_id',$page_id)->whereBetween('created_at',$start_date,$end_date)->get();
$qty = $clicks->count();