<?php

namespace App\Model;

use App\Exceptions\DatabaseException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

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

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

    protected $primaryKey = '_id';

    public static function mediaList($request){


        try{
            $where = [];

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

            $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;

    }

}
