<?php
/**
 *------------------------------------------------------
 * BaseAttachmentModel.php
 *------------------------------------------------------
 *
 * @author    m@9026.com
 * @date      2017/03/20 13:09
 * @version   V1.0
 *
 */

namespace App\Models;

use Illuminate\Http\UploadedFile;

class BaseAttachmentModel extends BaseModel
{
    //
    /**
     * 数据表名
     *
     * @var string
     *
     */
    protected $table = 'base_attachments';
    /**
    主键
     */
    protected $primaryKey = 'id';

    //分页
    protected $perPage = PAGE_NUMS;

    protected $guarded = [];

    public function upload(UploadedFile $file, $class = '')
    {
        $fileName = uniqid() . '.' . $file->getClientOriginalExtension();
        $date = date("Ymd");
        $res = $file->storeAs('upload/files' . '/' . $date, $fileName, 'upload');

        if($res) {
            $res = '/' . $res;
            self::create([
                'name' => $fileName,
                'path' => $res,
//                'type' => $file->getClientMimeType(),
                'class' => $class,
                'size' => $file->getClientSize()
            ]);
        }
        return $res;
    }
}
