当你在使用where语句有前提条件时,好比某值为1的时候才执行where子句,不然不执行,这个时候,laravel5.5新出了一个简便方法when($arg,fun1[,fun2])。html
具体用法以下:当$arg为真时,执行闭包fun1,为假时,执行闭包fun2(可选);laravel
举个栗子闭包
1 when($arg,function ($q){ 2 return $q->orderBy('id', 'asc'); 3 }, function ($q) use ($a){ 4 return $q->orderBy($a, 'desc'); 5 });
当$arg为真是,执行按id升序排序,当$a为假时,执行按$a降序排序。code
文章摘自 http://www.cnblogs.com/x-x-j/p/8397939.htmlhtm