love_php/app/Traits/Canlog.php

41 lines
1.0 KiB
PHP
Raw Permalink Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Traits;
use App\Models\Log;
use App\Contracts\Logable;
use Illuminate\Database\Eloquent\Relations\MorphMany;
trait Canlog
{
//增加访问记录
public function addlog(Logable $Logable,$channel=0) : Log
{
$Log = new Log([
'logable_id' => $this->primaryId(),
'logable_type' => get_class(),
'channel'=>$channel,
]);
$Logable->logs()->save($Log);
return $Log;
}
//访问记录
public function loglist() :MorphMany
{
return $this->morphMany(Log::class,'logable');
// return Log::where('logable_id',$this->primaryId())->where('logable_type',get_class())->get();
}
public function haslog(Logable $Logable)
{
$result = log::where('logable_id',$this->primaryId())
->where('logable_type',get_class())
->where('log_id',$Logable->primaryId())
->where('log_type', get_class($Logable))
->exists();
return $result;
}
}