<?php

namespace App\Model;

use App\Exceptions\DatabaseException;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Illuminate\Support\Facades\DB;

class MediaModel extends Eloquent
{
    protected $collection = 'media';

    protected $connection = 'mongodb';  //使用mongodb

    protected $primaryKey = '_id';

    public static function mediaList($request){


        try{
            $where = [];

            if ($request->name){
                $where['name'] = $request->name;
            }





//            MediaModel::insertGetId([
//
//                "_id"    => 'mda-'.generateRandomNum(16),
//                "url"    => 'http://www.baidu.com',
//                "QR_code"=> 'test',
//                'video_url'=>"http://www.sfdjlsdjflsaj,coanldsfjsalj.sdafljasdofjasp",
//                "create_time"   => date("Y-m-d H:i:s",time())
//
//            ]);
//
//            var_dump("fsd");die;






            $medias = DB::connection('mongodb')->collection('media')
                ->where($where)
                ->paginate(10);

//            foreach ( $medias as &$user){
//                $idArr = ($user['_id'])->jsonSerialize();
//                $user['_id'] = $idArr['$oid'];
//            }

        }catch (\Exception $exception){

            throw new DatabaseException($exception->getMessage());
        }

        return $medias;

    }

}
