这样写有错嘛?为什么控制器参数传不到视图?
控制器里的方法:
传递三个参数:
功能方法:
视图:
报错:
传递三个参数:
public function bePromotedStart($major_id){
return View("examinations.bePromotedStart", array('major_id'=>$major_id))
->withRandomGets($this->randomGet($major_id, 'bePromoted'))
->withMajors(Major::all());
}
功能方法:
public function randomGet($major_id, $examType) {
if ($examType = 'succeed') {
return Question::DifficultyHard()->where('major_id', $major_id)->orderByRaw("RAND()")->first();
}elseif ($examType = 'bePromoted') {
$resultEasy = Question::DifficultyEasy()->where('major_id', $major_id)->orderByRaw("RAND()")->first();
$resultNormal = Question::DifficultyNormal()->where('major_id', $major_id)->orderByRaw("RAND()")->first();
$resultHard = Question::DifficultyHard()->where('major_id', $major_id)->orderByRaw("RAND()")->first();
return array_merge($resultEasy, $resultNormal, $resultHard);
}else {
return "examType参数错误,只能是晋升(bePromoted)或续任(succeed)";
}
}
视图:
@forearch($randomGets as $randomGet)
<p>{{$randomGet->title}}</p>
@endforearch
报错:
Undefined variable: randomGet问题:如下这样就可以,为什么这里不能用->withRandomGets()?
->with("randomGet", $this->randomGet($major_id, 'bePromoted'))
1 个回复
雨师
赞同来自:
试试