1 回答
TA贡献1831条经验 获得超4个赞
我认为您应该将这两个课程结合起来。
尝试这个:
class CalculatorController extends Controller
{
public function calculatefunc(Request $request)
{
$operator = $request->input('operator');
$first_number = $request->input('first');
$second_number = $request->input('second');
$result = $this->calculateByOperator($operator, $first_number, $second_number);
return redirect('/')->with('info', 'Answer: ' . $result);
}
private function calculateByOperator($operator, $first_number, $second_number)
{
if ($operator == "plus") {
$result = $first_number + $second_number;
} elseif ($operator == "minus") {
$result = $first_number - $second_number;
} elseif ($operator == "multiply") {
$result = $first_number * $second_number;
} elseif ($operator == "divide") {
$result = $first_number / $second_number;
} else {
$result = 0;
}
return $result;
}
public function postResultCreate(Request $request)
{
$first_number = $request->input('first');
$second_number = $request->input('second');
$operator = $request->input('operator');
$result = $this->calculateByOperator($operator, $first_number, $second_number);
$post = new Result([
'first_number' => $first_number,
'second_number' => $second_number,
'operator' => $operator,
'result' => $result
// it should be result not sum because it will depend on your operator. you can also use operator here
]);
$post->save();
return redirect('/')->with('info', 'Answer: ' . $result);
}
}
- 1 回答
- 0 关注
- 97 浏览
添加回答
举报