LOADING

王思彤 发布的文章

$log = Invite::where('from_teacher_id',session('teacher')['id'])

        ->with('hasTeacher')
        ->with('hasBanjis')
        ->with('hasBanjis.hasBanjiStudents')
        ->with('hasBanjis.hasTasks.hasStudentHomeworks')
        ->get();

如果是hasmany就用循环,如果是hasOne就直接->用

public function up()

{
    Schema::create('activitys', function (Blueprint $table) {
        $table->increments('activity_id')->comment('活动表');
        $table->timestamps();
    });
    DB::statement("ALTER TABLE `tbl_activitys` comment'活动表'");//表注释一定加上前缀
}

public function headingCode()

{
    list($msec, $sec) = explode(' ', microtime());
    $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    $msectime = substr(str_shuffle($msectime), -6);
    $banji    = Table::where('heading_code', $msectime)->first();
    if($banji) {
       $this->headingCode();
    }
    return $msectime;

}

这是一个递归算法能随机6位数,并且去数据库中查询是否已有,没有的话就返回
但是这里有个问题, 就是递归时实际上第一个方法的变量还在内存中没有销毁,第二次进来的时候虽然变量都叫一个名字,但是内存中分配的是不同的空间进行存储,所以随后会出现只返回第一次的$msectime,解决方法如下

public function headingCode()

{
    list($msec, $sec) = explode(' ', microtime());
    $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    $msectime = substr(str_shuffle($msectime), -6);
    $banji    = Banji::where('heading_code', $msectime)->first();
    if($banji) {
        $msectime = $this->headingCode();
    }
    return $msectime;

}

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