LOADING

2019年3月

今天终于在项目中用到了goto特性

foreach ($coursewares as $key => $courseware) {

        st:
        $this_time = $new_times_tmp->where('c', $c)->first();
        if (!$this_time) {
            $c = $c + 1;
            $this_time = $new_times_tmp->where('c', $c)->first();
        }
        $new_times_tmp->where('c', $c)->first()->c = $this_time->c + 1;
        $day = $this->num_to_week($this_time->week);
        if($c==0){
            $begin_date = date('Y-m-d',strtotime('last '.$day));
        }else{
            $begin_date = date('Y-m-d',strtotime('+'.$c.' week last '.$day));
        }
        if ($festivals->where('festival_date', $begin_date)->first()) {
            goto st;
        }
        $new_cous->push([
            'cou_id'     => $courseware->id,
            'date'       => $begin_date,
            'begin_time' => $time_collect[$key]->begin_time,
            'end_time'   => $time_collect[$key]->end_time,
        ]);
    }

st:是声明一个作用域(只能在一个函数或类方法中使用),可以理解为标记,如果需要跳转到某一作用域时可以 用goto跳转
本来php作为解释语言是从上到下解释执行,有了goto就可以从下到上 从任意位置跳任意位置执行, 实在太方便了.

<?php
/**

  • 取得下周一时的结算区间
    */

class Project_View_Helper_TaskNotice
{

/**
 * @return string
 */
public function TaskNotice() {
    $nextMonday = $this->getNextMonday();
    $lastMonday = $this->getLastMonday();
    $lastSunday = $this->getLastSunday();
    $notice = '下周一('. $nextMonday .')结算'. $lastMonday .'至'. $lastSunday .'的款项。(如遇节假日顺延)';
    return $notice;
}

/**
 * 取得下个周一
 * @internal param $time
 */
private function getNextMonday()
{
    return date('m月d日',strtotime('+1 week last monday'));
}

/**
 * 取得上个周一
 * @return string
 */
private function getLastMonday()
{
    if (date('l',time()) == 'Monday') return date('m月d日',strtotime('last monday'));

    return date('m月d日',strtotime('-1 week last monday'));
}

/**
 * 取得上个周日
 * @return string
 */
private function getLastSunday()
{
    return date('m月d日',strtotime('last sunday'));
}

}

没毛病 php自带日期处理函数真的是强大!!!

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