第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

運(yùn)行一個函數(shù)并發(fā)布請求 Laravel

運(yùn)行一個函數(shù)并發(fā)布請求 Laravel

PHP
森林海 2023-04-21 16:40:23
Source 代碼 https://github.com/jslack2537/PHP-Calculator我在運(yùn)行我的計算器應(yīng)用程序的業(yè)務(wù)邏輯時遇到問題,同時將結(jié)果保存到我的數(shù)據(jù)庫中,它們獨(dú)立工作,只是不同時。我不確定我是否應(yīng)該組合我的兩個控制器或這里的什么。關(guān)于組合它們或使流程按原樣工作的最佳方式的任何建議。    <?phpnamespace 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);    }}我的第二個控制器具有基本的數(shù)學(xué)邏輯<?phpnamespace 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);    }}
查看完整描述

1 回答

?
慕容708150

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個贊

我認(rèn)為您應(yīng)該將這兩個課程結(jié)合起來。


嘗試這個:


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

    }

}


查看完整回答
反對 回復(fù) 2023-04-21
  • 1 回答
  • 0 關(guān)注
  • 123 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號