LOADING

分类 PHP 下的文章

$map['task_id'] = $request->task_id;
$finished = $request->finished;
$isShow = $request->is_show;
$isAll = $request->is_all ? $request->is_all: 0;
$limit = $request->limit ? $request->limit : 6;
if($isShow==1) {

$finished = '';

}
$data = TableModel::where($map)

->when($finished == 2 , function ($query) use ($finished) {//            未完成
    return $query->where('finished','!=','1');
})
->when($finished == 1  , function ($query) use ($finished) {//            已完成
    return $query->where('finished','1');
})
->when($isShow == 1 , function ($query) use ($isShow) {//              
    return $query->where('read_time','=','null');
})
->select('*')
->orderBy('num', 'desc')
->with('hasUser')
->when($isAll == 1 , function ($query) use ($isAll) {//              没有分页
    return $query->get();
})
->when($isAll != 1 , function ($query) use ($isAll,$limit) {//              有分页
    return $query->paginate($limit);
});

$keyword = $this->filters['keyword'];
$options = $this->parseOptions();
$datas = TableModel::with('hasBook')
    ->with('hasGradeCell.hasGrade')
    ->when($keyword,function($query)use($keyword){
        return $query->where('title','like','%'.$keyword.'%');
    })
    ->when($options,function ($query) use ($options){
        if (isset($options['cell_id'])) {
            return $query->where('cell_id', $options['cell_id']);
        }
        if (isset($options['grade_id'])) {
            return $query->whereHas('hasGradeCell',function ($query) use ($options){
                return $query->whereHas('hasGrade',function ($query) use ($options){
                    return $query->where('id',$options['grade_id']);
                });
            });
        }

    });
$counts            = $datas->count();

$datas = $datas->where('type', 1)
        ->orderBy($this->filters['sort'], $this->filters['sort_by'])
        ->paginate($this->filters['limit'])
        ->appends($this->filters);

try {

works::insertGetId($adata);
workContents::insertGetId($diyAdata);
DB::commit();

}catch (Exception $e){

DB::rollback();
Log::debug('添加失败'.'$e->getMessage:'.$e->getMessage().'|getCode:'.$e->getCode());
return output_error('添加失败');

}

public function upload(Request $request){

$imgs = [];
if ($request->hasFile('upload_img')){
    foreach ($request->file('upload_img') as $v){
        $time      = strtotime(date("Y-m-d 00:00:00"));
        $path = $v->move(public_path().'/upload/'.$time,$v->hashName());
        dump($path->getRealPath());die;
        $imgs[]= asset($path);
    }
    return response()->json([
        'errno'=>0,
        'data'=>$imgs
    ]);
}else{
    return response()->json([
        'info'=>'没有图片'
    ]);
}

}

use SoftDeletes;//使用软删除
/**

  • 隐藏字段(必有得参数不然返回值永远多一个hasOnetable)
  • @var array
    */

public $hidden = ['hasOnetable','hasTwotable'];

public function hasOnetable(){

return $this->hasOne('App\hasOnetable','id','one_id');//第一个是关联表主键,第二个是本表的外键

}

public function hasTwotable(){

return $this->hasOne('App\hasTwotable','two_table_id','id');

}

const limit = 6;//设置一个常量

$taskId = $request->task_id;
$type = $request->type;
$map['task_id'] = $taskId;
$map['finished'] = 2;
$isAll = $request->is_all ? $request->is_all: 0;
$limit = $request->limit ? $request->limit : self::limit;
$data = UserHomeworks::where($map)

->when($type == 2  , function ($query) use ($type) {//            未复读
    return $query->where('status',0);
})
->select('id','finished_time')
->orderBy('created_at', 'desc')
->with('hasOne')   //这部是为了之后循环的时候不每一次都查库,渴求式加载
->when($isAll == 1 , function ($query) use ($isAll) {//              没有分页
    return $query->get();
})
->when($isAll != 1 , function ($query) use ($isAll,$limit) {//              有分页
    return $query->paginate($limit);
});

在同事交流中使用设计模式描述程序思想,效率要快的多.
初学者可以把设计模式理解成一种专业术语, 就是为了沟通开发交流无障碍的,
所以设计模式是必学的
github 代码地址: https://github.com/baisecaihong/GOF
单例模式:
生成原因:1全局变量是无法复用的,如果A程序的全局变量没有增加到B程序中,那么你就无法在B中执行代码

          2全局变量将类捆绑与特定的环境,破坏了封装 

作用:用单例模式改进全局变量,因为无法用错误类型的数据覆写一个单例

<?php
//单例模式
class danli{

private $value = array();

private static $instance;

public static function getInstance()
{
    if(!(self::$instance)){
        self::$instance = new danli();
    }
    return self::$instance;
}

private function __construct()
{

}

public function __clone()
{
    // TODO: Implement __clone() method.
}

public function __wakeup()
{
    // TODO: Implement __wakeup() method.
}

public function setValue($key,$val){
    $this->value[$key] = $val;
}

public function getValue($key){
    return $this->value[$key];
}

}

$danli = danli::getInstance();
$danli->setValue('name','白色彩虹');
unset($danli);
var_dump($danli);
$danli2 = danli::getInstance();
print $danli2->getValue('name');

解释下代码
创建danli类
声明私有化变量$value数据类型为array,这一步就阻止了设置value为其他类型的覆写
声明私有化静态变量$instance 静态变量只存在于函数作用域内,静态变量只存活在栈中,下次再调用这个函数的时候,该变量的值会保留下来 而且私有化只能类内部访问
声明公共静态方法getInstance
如果$instance为空就重新实例化自己创建一个danli类实例并且返回实例
私有化初始化方法
私有化克隆
私有化wakeup
这些是用来保持数据一致性,防止被克隆之后操作混乱
共有的设置value 和提取value

使用:$danli引用danli类中的getInstance;
设置value值
删除引用
这个时候打印下$danli返回Undefined variable: danli
说明已经删除了,但是之前设置的value的值还在
所以$danli2再引用一次,
打印值 可以打印出来
$instance是不能被类外部访问的只能通过getInstance进行访问,又因为getInstance方法是public且static的所以脚本的任何地方都可以调用 这就形成了单例模式,

GET url
{

"query": {
"bool": {
  "must": [
    {"term": {
      "task_id.keyword": {
        "value": "130"
      }
    }},

     {"term": {
      "game_task_id": {
        "value": "1"
      }
    }}
  ]
}

}
, "size": 20
}