更新时间:2023-12-01 17:02:22
我不确定100%可以这样做,但是您应该可以执行类似的操作.它未经测试,并且可能会有更优雅的方法,但这是您的起点.
I'm not 100% sure on this, but you should be able to do something like this. It's not tested, and there may be a more elegant way to do it, but it's a starting point for you.
更改表格
将visit_date (datetime)
列更改为visit_date (date)
和visit_time (time)
列,然后创建一个id
列作为主键.最后,将ip + date
设置为唯一密钥,以确保一天之内两次输入相同的IP.
Change the visit_date (datetime)
column into visit_date (date)
and visit_time (time)
columns, then create an id
column to be the primary key. Lastly, set ip + date
to be a unique key to ensure you can't have the same IP entered twice for one day.
创建雄辩的模型
这很简单:为表创建一个Eloquent模型,这样您就不必一直使用Fluent(查询生成器):
This is just for ease: make an Eloquent model for the table so you don't have to use Fluent (query builder) all the time:
class Tracker extends Eloquent {
public $attributes = [ 'hits' => 0 ];
protected $fillable = [ 'ip', 'date' ];
protected $table = 'table_name';
public static function boot() {
// Any time the instance is updated (but not created)
static::saving( function ($tracker) {
$tracker->visit_time = date('H:i:s');
$tracker->hits++;
} );
}
public static function hit() {
static::firstOrCreate([
'ip' => $_SERVER['REMOTE_ADDR'],
'date' => date('Y-m-d'),
])->save();
}
}
现在,您只需调用以下命令便可以执行所需的操作:
Now you should be able to do what you want by just calling this:
Tracker::hit();