| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 | <?phpnamespace App\Libs;use Carbon\Carbon;use Illuminate\Database\Eloquent\Model;use Illuminate\Support\Facades\DB;class DoLog{    protected $name;    protected $modelOrDb;    /**     * @var \App\Http\Models\User     */    protected $user;    protected $on;    protected $log;    protected $addition;    public function __construct($name = null)    {        $this->name = $name ?? 'default';        return $this;    }    //使用哪个模型或数据库名记录    public function use($modelOrDb)    {        $this->modelOrDb = $modelOrDb;        return $this;    }        // 多数为user    public function by($user)    {        $this->user = $user;        return $this;    }    public function with($addition)    {        $this->addition = $addition;        return $this;    }    //记录    public function log(string $content = '')    {        if ($this->modelOrDb instanceof Model) {            $object = new $this->modelOrDb;        } else {            $object = DB::table($this->modelOrDb);        }        return $object->insert(            [                'name' => $this->name,                'content' => $content,                'addition' => $this->addition ?? '',                'operator_id' => empty($this->user) ? null : $this->user->getKey(),                'operator_name' => empty($this->user) ? null : $this->user->username(),                'created_at' => Carbon::now()->toDateTimeString(),                'updated_at' => Carbon::now()->toDateTimeString()            ]        );    }}
 |