41 lines
1.0 KiB
PHP
41 lines
1.0 KiB
PHP
<?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;
|
|
}
|
|
}
|