<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * 数据库操作模型类
 */
class Database extends Model
{


    /**
     * 循环获取or查询
     *
     * @access public
     * @param mixed $database 数据库
     * @param mixed $orWhere 或查询      [[['batch_id', '191'], ['batch_id', '186']],[['id', '55'], ['id', '59']]];
     * @return String
     */
    public function whereOr($database, $orWhere=[])
    {
        $result = $database;

        // 循环获取or查询.
        foreach ($orWhere as $value) {
            $result = $result->where(
                function ($query) use ($value) {
                    foreach ($value as $k => $v) {
                        if ($k === 0) {
                            $query = $query->where([$v[0] => $v[1]]);
                        } else {
                            $query = $query->orWhere([$v[0] => $v[1]]);
                        }
                    }
                }
            );
        }

        return $result;

    }//end whereOr()


    /**
     * 循环排序规则
     *
     * @access public
     * @param mixed $database 数据库
     * @param mixed $orderBy 排序字段   ['sort' => 'asc']
     * @return String
     */
    public function orderBy($database, $orderBy=[])
    {
        $result = $database;

        // 循环排序规则.
        foreach ($orderBy as $key => $value) {
            $result = $result->orderBy($key, $value);
        }

        return $result;

    }//end orderBy()


}
