运行一个函数并发布请求 Laravel

Source 代码 https://github.com/jslack2537/PHP-Calculator

我在运行我的计算器应用程序的业务逻辑时遇到问题,同时将结果保存到我的数据库中,它们独立工作,只是不同时。我不确定我是否应该组合我的两个控制器或这里的什么。关于组合它们或使流程按原样工作的最佳方式的任何建议。

    <?php


namespace App\Http\Controllers;


use App\Result;

use Illuminate\Http\Request;


class ResultController extends Controller

{

    public function postResultCreate(Request $request)

    {   $result = 2;

         $post = new Result([

             'first_number' => $request->input('first'),

             'second_number' => $request->input('second'),

             'operator' => $request->input('operator'),

             'sum' => $result

         ]);

        $post->save();

        return redirect('/')->with('info', 'Answer: ' .$result);

    }

}

我的第二个控制器具有基本的数学逻辑


<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;


class CalculatorController extends Controller

{

    public function calculatefunc(Request $request)

    {

        $operator = $request->input('operator');

        $first_number = $request->input('first');

        $second_number = $request->input('second');

        $result = 0;


        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 redirect('/')->with('info', 'Answer: ' .$result);

    }

}


森林海
浏览 98回答 1
1回答

慕容708150

我认为您应该将这两个课程结合起来。尝试这个:class CalculatorController extends Controller{&nbsp; &nbsp; public function calculatefunc(Request $request)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $operator = $request->input('operator');&nbsp; &nbsp; &nbsp; &nbsp; $first_number = $request->input('first');&nbsp; &nbsp; &nbsp; &nbsp; $second_number = $request->input('second');&nbsp; &nbsp; &nbsp; &nbsp; $result = $this->calculateByOperator($operator, $first_number, $second_number);&nbsp; &nbsp; &nbsp; &nbsp; return redirect('/')->with('info', 'Answer: ' . $result);&nbsp; &nbsp; }&nbsp; &nbsp; private function calculateByOperator($operator, $first_number, $second_number)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ($operator == "plus") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result = $first_number + $second_number;&nbsp; &nbsp; &nbsp; &nbsp; } elseif ($operator == "minus") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result = $first_number - $second_number;&nbsp; &nbsp; &nbsp; &nbsp; } elseif ($operator == "multiply") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result = $first_number * $second_number;&nbsp; &nbsp; &nbsp; &nbsp; } elseif ($operator == "divide") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result = $first_number / $second_number;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $result;&nbsp; &nbsp; }&nbsp; &nbsp; public function postResultCreate(Request $request)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $first_number = $request->input('first');&nbsp; &nbsp; &nbsp; &nbsp; $second_number = $request->input('second');&nbsp; &nbsp; &nbsp; &nbsp; $operator = $request->input('operator');&nbsp; &nbsp; &nbsp; &nbsp; $result = $this->calculateByOperator($operator, $first_number, $second_number);&nbsp; &nbsp; &nbsp; &nbsp; $post = new Result([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'first_number' => $first_number,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'second_number' => $second_number,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'operator' => $operator,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'result' => $result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // it should be result not sum because it will depend on your operator. you can also use operator here&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; $post->save();&nbsp; &nbsp; &nbsp; &nbsp; return redirect('/')->with('info', 'Answer: ' . $result);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP